3

I'm fairly new to PHP and was trying to create a simple PHP file upload system.

I followed a tutorial from (http://www.phpeasystep.com/phptu/2.html). I only altered the $HTTP_POST_FILES, as it was giving me errors, and from what I read it's old in PHP.

I got less error messages but I am getting an error in the copy() function, with these given error messages:

Warning: copy(Task2/uploads/anonymous.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\Task2\upload.php on line 13

Warning: copy(Task2/uploads/DSCF4639.JPG): failed to open stream: No such file or directory in C:\xampp\htdocs\Task2\upload.php on line 14

Warning: copy(Task2/uploads/jien maroon.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\Task2\upload.php on line 15

I thought it was a problem with permission (read/write permissions in Windows 7), but from a quick google search it seems that XAMPP is set by default to deal with permission on Win 7.

This is the code:

<?php

//set where you want to store files
//in this example we keep file in folder upload
//$_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif

$path1= "Task2/uploads/".$_FILES['ufile']['name'][0];
$path2= "Task2/uploads/".$_FILES['ufile']['name'][1];
$path3= "Task2/uploads/".$_FILES['ufile']['name'][2];

//copy file to where you want to store file
copy($_FILES['ufile']['tmp_name'][0], $path1);
copy($_FILES['ufile']['tmp_name'][1], $path2);
copy($_FILES['ufile']['tmp_name'][2], $path3);

//$_FILES['ufile']['name'] = file name
//$_FILES['ufile']['size'] = file size
//$_FILES['ufile']['type'] = type of file
echo "File Name :".$_FILES['ufile']['name'][0]."<BR/>";
echo "File Size :".$_FILES['ufile']['size'][0]."<BR/>";
echo "File Type :".$_FILES['ufile']['type'][0]."<BR/>";
echo "<img src=\"$path1\" width=\"150\" height=\"150\">";
echo "<P>";

echo "File Name :".$_FILES['ufile']['name'][1]."<BR/>";
echo "File Size :".$_FILES['ufile']['size'][1]."<BR/>";
echo "File Type :".$_FILES['ufile']['type'][1]."<BR/>";
echo "<img src=\"$path2\" width=\"150\" height=\"150\">";
echo "<P>";

echo "File Name :".$_FILES['ufile']['name'][2]."<BR/>";
echo "File Size :".$_FILES['ufile']['size'][2]."<BR/>";
echo "File Type :".$_FILES['ufile']['type'][2]."<BR/>";
echo "<img src=\"$path3\" width=\"150\" height=\"150\">";

///////////////////////////////////////////////////////

// Use this code to display the error or success.

$filesize1=$_FILES['ufile']['size'][0];
$filesize2=$_FILES['ufile']['size'][1];
$filesize3=$_FILES['ufile']['size'][2];

if($filesize1 && $filesize2 && $filesize3 != 0)
{
echo "We have recieved your files";
}

else {
echo "ERROR.....";
}

//////////////////////////////////////////////

// What files that have a problem? (if found)

if($filesize1==0) {
echo "There're something error in your first file";
echo "<BR />";
}

if($filesize2==0) {
echo "There're something error in your second file";
echo "<BR />";
}

if($filesize3==0) {
echo "There're something error in your third file";
echo "<BR />";
}
?>

Any help would be appreciated !

Thanks !

Aitor
  • 88
  • 1
  • 9
Brian
  • 1,951
  • 16
  • 56
  • 101
  • 2
    don't use `['name']` like that. a malicious user can embed pathing information in the upload data and scribble anywhere they want on your server. – Marc B Dec 03 '12 at 16:57
  • One often runs into this error, and to quickly troubleshoot it, follow these steps : https://stackoverflow.com/a/36577021/2873507 – Vic Seedoubleyew Apr 12 '16 at 15:52

3 Answers3

7

make sure that the destination directory exists, copy will not create directories for you.

The 2nd parameter is for file permissions and important for security, read more: https://wiki.archlinux.org/index.php/File_permissions_and_attributes

The 3rd parameter will also create recursive directories.

if (!is_dir($directory)) {
    mkdir($directory, 0777, true);
}
Sebastian Viereck
  • 5,455
  • 53
  • 53
3

Do not use copy, use move_uploaded_file(...)

Naftali
  • 144,921
  • 39
  • 244
  • 303
  • That gives me the same error and this `Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php87B9.tmp' to 'Task2/uploads/DSCF4639.JPG' in C:\xampp\htdocs\Task2\upload.php` Could it be because I'm using XAMPP and I'm uploading from my to PC to my PC ? – Brian Dec 03 '12 at 17:02
  • There is now a troubleshooting checklist for this frequent error here : https://stackoverflow.com/a/36577021/2873507 – Vic Seedoubleyew Apr 12 '16 at 15:52
0

Solved the problem !

The problem was with the Path.. instead of Task2/uploads/ I had to put ../Task2/uploads/.

Thanks!

Brian
  • 1,951
  • 16
  • 56
  • 101