6

Upload.php:

<?php

//This is the directory where images will be saved
$target = "pics";
$target = $target . basename( $_FILES['Filename']['name']);

//This gets all the other information from the form
$Filename=$_POST['Filename'];
$Description=$_POST['Description'];
$pic=($_FILES['Filename']['name']);


// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("altabotanikk") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO picture (Filename,Description)
VALUES ('$Filename', '$Description')") ;

//Writes the Filename to the server
if(move_uploaded_file($_FILES['Filename']['tmp_name'], $target)) {
    //Tells you if its all ok
    echo "The file ". basename( $_FILES['uploadedfile']['Filename']). " has been uploaded, and your information has been added to the directory";
} else {
    //Gives and error if its not
    echo "Sorry, there was a problem uploading your file.";
}
?>

And here is the form(in a separate file):

<form method="post" action="upload.php" enctype="multipart/form-data">
    <p>Photo:</p>
    <input type="file" name="Filename"> 
    <p>Description</p>
    <textarea rows="10" cols="35" name="Description"></textarea>
    <br/>
    <input TYPE="submit" name="upload" value="Add"/>
</form>

The errors are

 Undefined index: Filename on Line 17

(the $Filename=$_POST['Filename'];)

and

Undefined index: uploadedfile on Line 35

(the echo "The file ". basename( $_FILES['uploadedfile']['Filename']). " has been uploaded, and your information has been added to the directory";)

echo"<pre>".print_r($_FILES,true)."</pre>";

gives me:

Array
(
    [Filename] => Array
        (
            [name] => Laserkanon.jpg
            [type] => image/jpeg
            [tmp_name] => C:\WampServer\tmp\php11D4.tmp
            [error] => 0
            [size] => 41813
        )

)
16
  • $_POST['Filename'] does not exist. $_FILES['uploadedfile'] does not exist. $_FILES['Filename'] exists. You may want to echo the value of $_FILES to see the array structure: echo"<pre>".print_r($_FILES,true)."</pre>"; Commented Jun 17, 2013 at 17:52
  • If I had ANY idea whatsoever you actually meant there... where does that snippet go? And thanks, lopez.mikhael Commented Jun 17, 2013 at 17:54
  • 2
    in upload.php you should use $_FILES to get file info instead $_POST['filename']. if you want to name this file from the form you should put input type=text name="filename... Commented Jun 17, 2013 at 17:55
  • You should re order the idea, first move the file into the directory, second save it on the Database. Commented Jun 17, 2013 at 17:55
  • 2
    The mysql_* functions are old and will soon be removed from PHP entirely. Update your code to use PDO or mysqli so that it will continue to work in the future. Commented Jun 17, 2013 at 19:14

2 Answers 2

8

First you should use print_r($_FILES) to debug, and see what it contains. :

your uploads.php would look like:

//This is the directory where images will be saved
$target = "pics/";
$target = $target . basename( $_FILES['Filename']['name']);

//This gets all the other information from the form
$Filename=basename( $_FILES['Filename']['name']);
$Description=$_POST['Description'];


//Writes the Filename to the server
if(move_uploaded_file($_FILES['Filename']['tmp_name'], $target)) {
    //Tells you if its all ok
    echo "The file ". basename( $_FILES['Filename']['name']). " has been uploaded, and your information has been added to the directory";
    // Connects to your Database
    mysql_connect("localhost", "root", "") or die(mysql_error()) ;
    mysql_select_db("altabotanikk") or die(mysql_error()) ;

    //Writes the information to the database
    mysql_query("INSERT INTO picture (Filename,Description)
    VALUES ('$Filename', '$Description')") ;
} else {
    //Gives and error if its not
    echo "Sorry, there was a problem uploading your file.";
}



?>

EDIT: Since this is old post, currently it is strongly recommended to use either mysqli or pdo instead mysql_ functions in php

Sign up to request clarification or add additional context in comments.

9 Comments

Nono, not trying to rename the file, I am trying to upload the file and a description, with it's original filename
According to your $_FILES array, the original filename is stored in $_FILES['Filename']['name'].
@bksi Yeah I do, now I have 5 errors instead of one :/ Undefined index srcFile and move_uploaded_file() [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: The second argument to copy() function cannot be a directory + move_uploaded_file() [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: Unable to move 'C:\WampServer\tmp\php7068.tmp' to 'pics'
Yeah my mistake, someone tried to edit my post and returned it with one version. Try with this one. In your form use <input type="file" name="Filename"/>
You can;t have undefined index srcFile due i removed it from the code
|
-3
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("altabotanikk") or die(mysql_error()) ;

These are deprecated use the following..

 // Connects to your Database
            $link = mysqli_connect("localhost", "root", "", "");

and to insert data use the following

 $sql = "INSERT INTO  Table-Name (Column-Name)
VALUES ('$filename')" ;

1 Comment

Answering a 5 year old question with accepted answer to say the code is deprecated is not very useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.