Image Upload Error in PHP

3

I'm getting the error below when I upload an image, but I'm not able to identify what I have to fix.

Warning: move_uploaded_file(/uploads/1432585475.png) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/storage/b/71/d0/site1390582818/public_html/upimgsc.php on line 40

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/home/storage/b/71/d0/site1390582818/tmp/php0CABDK' to '/uploads/1432585475.png' in /home/storage/b/71/d0/site1390582818/public_html/upimgsc.php on line 40
    
asked by anonymous 25.05.2015 / 22:31

3 answers

1

Man, apparently the error says that this /uploads folder is not being found.

No such file or directory

Does /uploads exist anyway? Is this folder at the root?

    
25.05.2015 / 22:45
3

Before uploading your image, create the directory if it does not exist.

After the verification code you can upload it.

$folder = 'seu/diretorio/uploads';
if (!is_dir($folder)) {
    mkdir($folder, 0777, true);
}
    
25.05.2015 / 22:56
3

The move_uploaded_file() needs 2 arguments.

The temporary file destination (which is defined in PHP.ini as the temporary directory for uploads) and the final destination, indicated by you.

Note that this board already has to exist. If it does not already exist (and with the right permissions) you have to create it. You can create the board as @Diego suggested .

$temp  = $_FILES["new_image"]["tmp_name"];
$error = $_FILES["new_image"]["error"];

if ($error > 0) die("Algo correu mal!... code $error.");
move_uploaded_file($temp, $finalPath); // $finalPath defenida por tí

Keep in mind that uploading uploaded files within public_html is not safe. At least check if the file is an image like this:

$temp  = $_FILES["new_image"]["tmp_name"];
$error = $_FILES["new_image"]["error"];
$name =  $_FILES["new_image"]["name"];

$uploadPath = "/home/storage/b/71/d0/site1390582818/upload/".$name;
$finalPath  = "/home/storage/b/71/d0/site1390582818/public_html/upload/".$name;

if ($error > 0) die("Algo correu mal!... code $error.");
move_uploaded_file($temp, $uploadPath);

$size = getimagesize($uploadPath);
if(!$size) die('Algo correu mal!...');
rename($uploadPath, $finalPath);
    
25.05.2015 / 22:57