PHP Image Upload Error

Asked

Viewed 741 times

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

3 answers

3

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

After the code checking you can upload.

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

3

The move_uploaded_file() needs 2 arguments.

The temporary file destination (which is closed in PHP.ini as a temporary uploading directory) and the final destination, indicated by you.

Note that this board already has to exist. If it does not exist yet (and with the right permissions) you must 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 it is not safe to upload uploaded files into public_html. 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);
  • The upload worked, but in the "photo" field of the table...

  • @Valloto as well in the "photo" field of the table? you can explain better?

  • I am saving the path and the name of the image in the picture table.. in the photo column! Getting as follows ' path/image.png '

  • @Valloto "table" and "column" is used in databases like Mysql, is that what you’re talking about? or do you mean folder/file? in the case of folder/file you can change the path you want in the variables $finalPath and $uploadPath and the code will do what you want.

  • I was able to make the recording and also visualize the images, the code ended up being very simple, I will finish the service and put the code for you to help me with patterns. Thank you

1


Dude, apparently the mistake says this folder /uploads is not being found.

No such file or directory

The briefcase /uploads is there really ? This folder is in the root ?

  • Hello Diego.... Yes, it’s at the root!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.