PHP: move_uploaded_file () could not open the stream: there is no such file or directory

I am trying to get PHP to move the downloaded file from the tmp directory to a permanent location on my web server. This seems simple enough, but I get this error:

Unable to move 'C:\UniServer\tmp\php3F62.tmp' to 'static/images/slides/1/1.jpg'

Pretty straightforward, right? He cannot find the destination folder.

My question is: How do I specify the desired destination directory?

Is the link relative to the position of the script on the server? Or is it relative to the url? Or PHP DOCUMENT_ROOT ? Or an OS file system? Or something else?

I cannot find the answer in the PHP documentation or even in any of the similar questions here on SO ..

Can anyone help? Thanks.

+11
php apache file-upload
source share
4 answers

An easy way to track the path is to determine the absolute path in index.php

 define ('SITE_ROOT', realpath(dirname(__FILE__))); 

Then just use it like:

 move_uploaded_file($_FILES['file']['tmp_name'], SITE_ROOT.'/static/images/slides/1/1.jpg'); 
+27
source share

I had the same problem with my download. See my example and maybe it can help you.

The file I created is called "uploads".

 $uploads_dir = 'uploads/'; $name = $_FILES['userfile']['name']; if (is_uploaded_file($_FILES['userfile']['tmp_name'])) { //in case you want to move the file in uploads directory move_uploaded_file($_FILES['userfile']['tmp_name'], $uploads_dir.$name); echo 'moved file to destination directory'; exit; } 
+2
source share

This is from the position of the script on the server! And what's more, you need to have write permissions in this folder:

 'static/images/slides/1/1.jpg' 

Instead, it is better to use an absolute path this way:

 'C:\UniServer\***\static\images\slides\1\1.jpg 

Use the absolute path.

0
source share
 $destination = dirname(dirname(dirname(dirname(__FILE__))))."/runtime/tmp/"; chown($destination, 0755); move_uploaded_file($info['tmp_name'], $destination.$info['name']); 

This is my solution, I just use mkdir to create a directory to put my photo that I want to move. Wish it helps.

0
source share

All Articles