What I have right now for file upload is:
move_uploaded_file($filetemp, "files/$filename");
With filetemp referring to $_FILES['fileupload']['tmp_name'], filename referring to $_FILES['fileupload']['name'], and files referring to a folder of that name inside the folder where the PHP file is.
However, this does not move the file to the files folder. How do I make it so that the function moves the file there?
Thanks!
Nerd With a Vengeance
There could be any number of reasons why this might not be working.
The first thing to check is permissions - make sure the webserver has write permissions to the directory you're trying to write to.
Also, turn your error reporting up - see what warnings are being generated on failure (assuming that you're return value is indeed false).
Related
So, I have a script in
/var/www/vhosts/Domain/SubDomain/Script.php
that writes images to
/var/www/vhosts/Domain/wwwDomain/PhotoLocation/
and this works fine.
However, when I run this script:
/var/www/vhosts/Domain/SubDomain/Script_to_Delete_Photo.php
which uses the 'unlink' command
unlink ("/var/www/vhosts/Domain/wwwDomain/PhotoLocation/Image.jpg")
I get the error
"PHP Warning" "No such file or directory in
/var/www/vhosts/Domain/SubDomain/Script_to_Delete_Photo.php"
I thought that since I could WRITE the file fon a different subdomain, I could Delete from the other subdomain as well.
am I missing something?
Do I need to set Permissions somewhere else, or set a path differently? I specifically call the File by Absolute Path, and I can verify that the file exists there.
All looks fine. Some suggestions:
files are case sensitive
check if the folder Photolocation has write permission (it usually will for you can write to the location)
close the handle after you've created the file. If the handle is still open, delete is not possible
check with file_exists() just to make sure the file does exist
did you create the file or a shortcut?
when creating the file, do not suppress possible errors. It might be a header problem in the created file itself. Check your error-files for clues.
Hi I want to create folders (with php) outside of the webroot and move uploaded files to them.
is that possible?how?
Use rename, copy or move_uploaded_file, though you need to make sure the folder has the correct permissions (write permissions for your webserver / php executing user).
Also, Handling file uploads might have some useful information for you.
Use php's move_uploaded_file() function.
Linked in a comment, however it bears repeating: Read the documentation.
Check if the directory exists and if not, create it:
if (!is_dir('/dir/path')) {
mkdir('/dir/path');
}
Move your uploaded file to the directory:
move_uploaded_file($_FILES["file"]["tmp_name"], "/dir/path");
I used to have a php file that does a simple move_uploaded_file by using selecting a local file and upload to our UNIX web server.
Now we migrate our code to a Windows2003 Server, then the move_uploaded_file() fails, the error that keeps coming up reads like:
"Cannot move the C:Windows\temp\100D.php" file to desiredDirectory.
here desiredDirectory means it caputures the correct directory for this file movement. The code we used is pretty straightforward:
if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
and we did try change it to $HTTP_POST_FILES, but still not working.
So we are really clueless at the moment, wonder if any experts could give us some hints, thanks a lot.
You should check if the target directory exists and if the apache user has all rights on that folder.
For a test you can set the folder access settings for the user 'everyone' to 'full'
The snippet of your code i see here is correct and you don't have to use $HTTP_POST_FILES
Does the webserver have write permissions on the target directory? Given that you say the paths are correct, that's the other #1 major reason why file moves fail.
I just want to know that if I am using move_uploaded_file function and use two argument first as the name of file and second as the destination.
Normally I have uploaded many files with class uploader but now I want to give the destination as http://www.example.com/testing/
Although I have given 777 permission to this folder but when I try to execute the upload code error came
Destination directory can't be created. Can't carry on a process.
How can I upload the file local to server using php code?
If you are passing http://www.mydomain.com/testing/ as the target, this is wrong.
You can't just upload files to servers via HTTP, you only can do that to local folders, can you paste the exact code so we can know better what are you trying to do?
move_uploaded_file is a server-side function, so all the paths should be specified server side.
If your upload.php (i'm assuming the filename) is in the main directory of the website www.mydomain.com/ which is probably /home/youruser/public_html/ then you can specify the destination as simply "testing/"
If your upload file is in some nested directory, then it may work better to specify the full destination path:
/home/youruser/public_html/testing
good luck
I'm uploading files via JS and storing the temp path in the session.
Than i use the following code to move the files.
if(move_uploaded_file($_SESSION['temp_img'][$key]['path'], $dest.$bigimg)){
$dest and $bigimg are defined earlier in the script with the id from the database.
Any Ideas or alternatives ?
MANCHUCK's answer was close but not quite there. You must call move_uploaded_file within the script where the file was uploaded. You cannot do what you're doing, that is, "storing temp path in the session" because that path is only valid for one request.
From the PHP manual:
The file will be deleted from the
temporary directory at the end of the
request if it has not been moved away
or renamed.
(Emphasis mine)
move_uploaded_file checks that a file has been uploaded to that page. You are actually uploading the file to a different PHP script then storing in a session. Instead of using move_upload_file use rename.
What is the output of $_SESSION['temp_img'][$key]['path'], also do you have permission to write to the web directory your placing the files. You may need to set it to 777 for some hosts to allow the webserver to write there.