I'm trying to upload a file (msword/doc) to a Apache server folder via an HTML form. It works when I test it locally (I'm testing it via MAMP), but when I upload it to a remote server (such as GoDaddy), it doesn't work. It shows "There was a problem with the file upload".
Below is the snippet of code that processes the file upload. I can't figure out what is wrong with my conditional.
// Move the file to the target upload folder
$target = FILE_UPLOADPATH . basename($new_file);
if (move_uploaded_file($_FILES['new_file']['tmp_name'], $target))
{
// The new file move was successful, now make sure any old file is deleted
if (!empty($old_file) && ($old_file != $new_file))
{
#unlink(FILE_UPLOADPATH . $old_file);
}
}
else
{
// The new file move failed, so delete the temporary file and set the error flag
#unlink($_FILES['new_file']['tmp_name']);
echo 'There was a problem with the file upload.' . PHP_EOL;
}
Are you sure that the folder you are uploading to has permission for files to be written to it? If not, use chmod 0777 and test with that.
Does your destination folder have proper permissions? http://en.wikipedia.org/wiki/Chmod The directory write to typically needs 775: What are the proper permissions for an upload folder with PHP/Apache?
Also, do you want users to have direct access to the file? If not you should consider writing the file to a folder that is above your web root directory.
If $_FILES['new_file']['error'] == 0 then the upload isn't the problem, but the call to move_uploaded_file() is. You probably have incorrect permissions on the directory you're trying to move the file to.
For me, I happened to be testing locally with a file under php's upload_max_filesize while testing remotely with a file over upload_max_filesize. See https://stackoverflow.com/a/30359278/3325776 for more info.
Related
I have a php site hosted in aws ec2 server.
While uploading files using php script it have only 0644(rw-r--r--) permissions in server. i need to change the file permissions to 0777(rwxrwxrwx).
What am doing is .. uploading a file and then read it and create a new encripted file using it and then delete the first file. its not happening because of file permission issue in aws uploading file through script.
uploaded files shows owner as 'apache' and transfer file using winSCP shows 'ec2-user'.
please help , am not much experinced in aws , so please please send your answers in detail.
Try This one.
Hope it will work
move_uploaded_file($temp_file, $UploadDirectory.$NewFileName );
chmod($UploadDirectory.$NewFileName, 0755);
$in_filename = $UploadDirectory.$NewFileName;
$aes_filename = //your path to new file with name;
$this->encryptFileCBC($in_filename, $aes_filename);
unlink($UploadDirectory.$NewFileName);
So I have code for uploading image to server that actually work when I tested it on the windows system, but as soon as I put it on Linux, it is doesn't work. So problem is that it doesn't give me any mistake, in the end it put the link on data base, but doesn't put the image in the folder where it spouse to be. So the problem is only in uploading image in server.
So here is the code where the path and image download.
$path = "../users/".$IDN."/";
if(preg_match('/[.](JPG)|(jpg)|(jpeg)|(JPEG)|(gif)|(GIF)|(png)|(PNG)$/',$_FILES['imgupload']['name']))
{
$filename = $_FILES['imgupload']['name'];
$source = $_FILES['imgupload']['tmp_name'];
$target = $path.$filename;
move_uploaded_file($source, $target);
// and other funny actions
What is the owner/group of that ../users/$IDN? Could you print out the output of following command:
ls -l ../users
Also what is your web service? Is it apache? What username your web server is running with?
If your web service user does not have permission to write in target directory, then you can't really write unless you change permission.
Also try to change error_reporting to ALL in your php.ini to see the error message.
I am trying to create a really simple webpage in php, letting people upload images to a folder on my server. I made this really simple with some done code, and it worked awesomely on my computer with xampp, but when I upload the page to my server, it gets an error message every time I upload anything. The error is when the script checks if the image was uploaded, where it says
$copied = copy($_FILES['image']['tmp_name'], $newname);
if(!$copied)
echo "error";
This leads me to believe that there is something wrong with the permissions. But how can I set this? And what should I set it to? I just need others to be able to upload images to a spesific folder.
The web server needs write permissions to be able to write into the directory you're storing the images.
Assuming you're on a Linux server, run the following command on the server (ssh) after changing /path/to/uploaded/images to the image upload directory, and see if it solves the problem:
chmod 777 /path/to/uploaded/images
If that fixes the problem, you can probably relax the permissions to something like:
chmod 664 /path/to/uploaded/images
These are basic commands for directory permissions, which you can learn more about in this tutorial about file permissions on Linux.
Alternatively, you can use move_uploaded_file() to copy the uploaded file to a known location.
I have a few image upload scripts and wordpress blog as well.
I think the client tried to upload a 3mb+ images or so and since then everythings stopped working, to further inspection and adding error handlers i found out that
it was UPLOAD TEMP DIR not FOUND
i cant touch the ini file, its shared hosting but i can create a local ini file
i've been told to add php.ini in the public_html folder
it only works i.e prints out the upload_tmp_dir when its in the same folder as the script anyway
i put this in the ini file
upload_tmp_dir = "/home/USER/tmp"
also tried
upload_tmp_dir = "/home/USER/public_html/tmp"
but the first one is where the tmp folder was but i tried creating it else where with 777 permissions
leave ini in same folder as the script and calling
echo ini_get('upload_tmp_dir');
gives me: [given i set it to that]
/home/USER/tmp
sys_get_temp_dir();
gave me
/tmp
anyway NOW i get no errors, says file is uploaded but its no where to be found.
not sure WTF is going on but clients wrecking my head and so is this.
PHP will delete uploaded files when the script exits, unless you've moved/copied the file somewhere else yourself. The upload process isn't a "do it now and come back later to deal with it" system. It's "deal with it now, or it's gone".
To retrieve the file's temporary storage location/name, you use $_FILES['namegiveninform']['tmp_name'], and generally would use move_uploaded_file() to move the file into its permanent storage location.
i'm working on a website wherein the users can upload images (uses php 4.3.11). the files are uploaded with no problem as i can see them in the upload directory and i don't get any error message, but when i try to access the uploaded files via ftp, i get an error: no such file or directory. sometimes, i am able to access the file sometimes i get this error. what could be the problem here?
[update]
thanks for the help guys. i'm not familiar with the ftp daemon stuff. but i do access my files via ftp using FireFTP. the files are there but when try to download them or change the file properties, i get the said error. i also tried uploading a file in the folder through ftp and i was able to download it with no problem.
here is some of the code i'm working on, its kind of roundabout but i'll see on how to improve it.
my working directory is something like this www.domain.com/register/
and the upload directory is here www.domain.com/register/uploads/
users are required to register and upon sign-up, a folder is created for them in the uploads directory. i couldn't find a way to create a folder without having to be in the uploads folder itself so i redirect to a create-user-folder.php file in the uploads dir.
the file just contained this code:
$user_foldername = rawurldecode($_GET['name']);
mkdir($user_foldername);
header("Location: ../form.php"); // redirect back to the page
i checked and the created folder's permission is set to 775.
and here's part of the code i use in uploading ( /register/function/function.php ):
$path = "../uploads/$user_foldername/";
for($j = 0; $j < $num_of_uploads; $j++){
if(is_uploaded_file($_FILES[$file]['tmp_name'][$j])){
$filename = $_FILES[$file]['name'][$j];
copy($_FILES[$file]['tmp_name'][$j],$path.$filename);
}
}
i checked using FireFTP and the files are in the /uploads/user_foldername/ directory and its permission is set to 664. the strange thing is that when i try to download the files, at times there would be no problem at all but there are times when the error will appear.
[another update]
i added chmod() after the copy() function,
$filename = $_FILES[$file]['name'][$j];
copy($_FILES[$file]['tmp_name'][$j],$path.$filename);
chmod($path.$filename, 0755);
but i still get the error.
another thing is that when i access /register/uploads/user_foldername/ through the url, i can see all of the uploaded files and view them, but how is it that i can't access them via ftp?
thanks again!
This is either a permission issue, or a configuration error. Here are things you should try:
What are the permission of the uploaded files? Does the FTP user has access to these files? Have you tried logging in as the user the FTP daemon would use and see if you could read the file that way?
Do you really see the correct directory? Have you verified by putting a file in that directory yourself and downloading it? Have you used the ftp command ls to verify the presence of the folder/folders/files?
You might need to chmod the folder the files are in, or in some cases the files themselves.
try chmoding them to 775
You can chmod files and folders through PHP it's self, with the chmod function. Or, you could use a FTP program such as filezilla.
Also check to make sure the intermediate directories are also permissioned as 755, as all the directories in the path need to be executable to be traversed.
i just figured out the problem. it was all because of the file name having accented characters in it, which explains why i do not always get the error message :|
<sigh> i should have seen this earlier, but anyway i hope this helps in case someone ran into the same problem.
thanks again! i really appreciate it :)