0
votes

I tried uploading a picture to a directory in my server with the code below. However, when i run it, I get this error:

Warning: move_uploaded_file(images/) [function.move-uploaded-file]: failed to open stream: Is a directory in /home/a2943534/public_html/add.php on line 24

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php7yEkDe' to 'images/' in /home/a2943534/public_html/add.php on line 24

What am I missing here, please?

<?php
include_once("connect.php");
?> 
<?php 

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

 //This gets all the other information from the form 
 $title=$_POST['title'];
 $name=$_POST['name']; 
 $describe=$_POST['describe']; 
 $pic=($_FILES['photo']['title']);
 $url=$_POST['url'];
 $country=$_POST['country'];
 $endDate=$_POST['endDate']; 


 //Writes the information to the database 
 mysql_query("INSERT INTO `authors` VALUES ('$title', '$name', '$describe', '$pic', '$url', '$country', '$endDate')") ; 

 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
  $result =  "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
 } 
 else { 

 //Gives and error if its not 
 $result =  "Sorry, there was a problem uploading your file."; 
 } 
 ?> 
<?php
// if the form has been submitted, display result
if (isset($result)) {
  echo "<p><strong>$result</strong></p>";
  }
?>
4
make sure that the images directory really exists. - Shakti Singh
Also make sure that the images directory has write permission. - JRSofty
For a start you're missing sanitization, resulting in an arbitrary file write vulnerability. See stackoverflow.com/questions/1911382/sanitize-file-path-in-php - Polynomial
@JRSofty I am curious, have you ever care to read this question? - Your Common Sense

4 Answers

0
votes

rather than writing

$target = $target . basename( $_FILES['photo']['title']);

you should write

$target = $target . basename( $_FILES['photo']['name']); 

i think there is nothing like $_FILES['photo']['title']..

0
votes

I think you make a mistake with

$target = $target . basename( $_FILES['photo']['title']); 

Which should be

$target = $target . basename( $_FILES['photo']['name']); 

This because title does not exists within the $_FILES['photo']

Also this error states it:

Unable to move '/tmp/php7yEkDe' to 'images/' in /home/a2943534/public_html/add.php on line 24

The to images/ does not contain your filename.

0
votes

the error is obvious and self-explanatory.
the second parameter have to be a filename, not directory.

0
votes

Apart from the SQL injection issue which seems to be present in every single PHP question that features MySQL here on SO, you should start debugging.

You're getting a pretty clear error on which line and what function call causes the error. Look the error up on Google, read the manual for the functions you're using.

Long story short: you should create two variables, print them before your function call, and figure out what's wrong.

<?php

$source = $_FILES['photo']['tmp_name'];
$target = "images/" . basename( $_FILES['photo']['title']); 

echo "Moving '$source' to '$target'";

move_uploaded_file($source, $target);

You'll immediately see where the error occurs.