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
        )

)
halfer
  • 19,824
  • 17
  • 99
  • 186
The Last Melody
  • 165
  • 1
  • 3
  • 10
  • $_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"
    ".print_r($_FILES,true)."
    ";`
    – showdev Jun 17 '13 at 17:52
  • If I had ANY idea whatsoever you actually meant there... where does that snippet go? And thanks, lopez.mikhael – The Last Melody Jun 17 '13 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... – bksi Jun 17 '13 at 17:55
  • You should re order the idea, first move the file into the directory, second save it on the Database. – ExoticSeagull Jun 17 '13 at 17:55
  • Yupp, got rid of the first error... Still so much to learn... – The Last Melody Jun 17 '13 at 17:56
  • Nurdglaw, I do not have any idea whatsoever on how to fix any errors... The errors are not supposed to be there... – The Last Melody Jun 17 '13 at 17:57
  • use print_r($_FILES) in your upload.php to see what it contents – bksi Jun 17 '13 at 17:59
  • @ClaudioLudovicoPanetta what? – The Last Melody Jun 17 '13 at 17:59
  • To clarify my previous comment: The errors indicate that variables you're trying to access are undefined. `$_POST['filename']` does not appear in your form. `$_FILES['uploadedfile']` does not appear in your form. Since you have not defined them, they are undefined. – showdev Jun 17 '13 at 18:01
  • @TheLastMelody yes because think this: You save the file and then you move it but if the move fails for any reason? On your DB you have the row BUT the files is not there! – ExoticSeagull Jun 17 '13 at 18:02
  • @showdev well, how do I define them? – The Last Melody Jun 17 '13 at 18:03
  • @bksi thanks, updated post with result, but it tells me error is = or less than 0... – The Last Melody Jun 17 '13 at 18:04
  • @ClaudioLudovicoPanetta Yes, so, if I swap the places of the INSERT INTO and the (move_uploaded), it would be better? – The Last Melody Jun 17 '13 at 18:05
  • @TheLastMelody at least you don't have the problem of "ghost files" in your DB. – ExoticSeagull Jun 17 '13 at 18:06
  • @ClaudioLudovicoPanetta And I hope I never will, but knowing myself, I will get there too... – The Last Melody Jun 17 '13 at 18:16
  • 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. – FireCrakcer37 Jun 17 '13 at 19:14

2 Answers2

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

bksi
  • 1,606
  • 1
  • 23
  • 45
-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')" ;
Shahid Amin
  • 41
  • 11
  • Answering a 5 year old question with accepted answer to say the code is deprecated is not very useful. – miken32 Nov 14 '19 at 21:00