uploading file doesn't work in linux - php

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.

Related

Upload big file not working

As the title say I have a problem with uploading video.
I need to make a form that upload a file into a folder, it work with photos, and even with videos (or anyway, .mp4 file).
The problem comes when I try to upload big files, because PhP just fail.
I already modified php.ini post max size, max filesize, memory limit and even the time to process script or to input it, but if won't work anyway.
A strange things that I have notice is that when I try to upload big files, looks like php miss name and extension of the file, even if it works perfectly with small files.
How can I fix this? Is there any PhP command to set on the script, or something else in php.ini, looks like the php guide won't help me.Thank's
$extension = pathinfo($_FILES[image]['name'], PATHINFO_EXTENSION);
$target = "uploaded/";
$target = $target . $title.'.'.$extension;
$pic=($_FILES['image']['name']);
//Writes the photo to the server
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
} else {}
As per our discussion in the comments, it appears that you missed restarting the web server after making your php.ini changes. This file is read in at the start of the web server process, not each time a page is accessed. So any changes to this file require a web server restart before they'll take affect.
To be clear, restarting the web server refers to the web server service, not the OS on the server.

File Upload (works locally, but not on remote server)

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.

Setting permissions in PHP on server

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.

Uploaded file doesn't have a file owner (using move_uploaded_file in PHP)

I am using the move_uploaded_file function to upload files. The files get moved into the right directory and I see its physical existence, but I can't open them. I get "Access Denied" error. I view the properties of the file and I found that it doesn't have an owner.
By the way, I am running IIS on Windows 2008.
I check the permissions, everything is set up correctly. The user that is running the web service has full admin rights.
Additionally, I replaced the move_uploaded_file function with copy(), and I was able to access the file. It also had the user that is running the server as its owner.
What on earth could possibly be wrong?
Thanks I appreciate your help.
UPDATE:
After the file is moved I tried to set permissions using chmod($filename,0655) but that didn't work either.
UPDATE 2 (solution):
Alright guys, I figured it out.
The system administrator didn't set the upload_tmp_dir.It worked once we added a path.
Source code:
// Check if file was uploaded
if(isset($_FILES['formname']) && $_FILES['formname']['size'] > 8){
$displayName = $_FILES['formname']['name'];
$displayName = unixfilename(basename($displayName));
$tempName = $_FILES['formname']['tmp_name'];
$filename = $_FILES['formname']['name'];
$fileType = substr($filename, strrpos($filename,".")+1);
$filename = substr($filename,0,strrpos($filename,".")) . date("_Ymd_His") . ".$fileType"; // Add unique identifier
$filename = unixfilename(basename($filename));
$destinationPath = "xx\yy\zz\\";
// Check if the file is of a valid type
if($fileType == "txt"){
// Check if file exists in processor folder
if(!file_exists($destinationPath.$filename)){
if(move_uploaded_file($tempName,$destinationPath.$filename)){
if(file_exists($destinationPath.$filename)){
// success
check who is the user who do the upload actually
echo exec('whoami');
In addition to having upload_tmp_dir set in your php.ini file, ensure that the directory you set is writable by PHP.
If you're using the PHP Manager in IIS, it defaults the upload_tmp_dir to C:\Windows\Temp which was not writable by the PHP process by default for me.

jQuery uploadify I/O error UNIX

I've a problem with jQuery uploadify script and I didn't found any solution.
I've integrated this script on my project and everything is working fine on a Windows server(localhost) but when I try to run it on an UNIX server and I/O error is risen.
This only happens when I try to upload a file that already exists on uploading folder. On Windows the file is overwritten but a UNIX I get and I/O error.
Please if you have any solutions I'll be very grateful.
Here is the server side code which I think is the problem(PHP code):
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'].$_REQUEST['folder'].'/';
$targetFile = str_replace('//', '/', $targetPath).$_FILES['Filedata']['name'];
if (file_exists($targetFile)) unlink($targetFile);
copy($tempFile, $targetFile);
echo "1";
}
First few things to check:
Which user owns the file that exists?
Which user owns the directory that the file is in?
Which user is running your PHP script?
What are the permissions on the file itself?
What are the permissions on the directory that the file is in?
The reason I ask these questions is because it may be a simple permissions problem. The user running the unlink and file create of the target file will need write access to the directory. It's possible that the user that's running the script is not the one that owns the directory or file, in which case you may have to open up the permissions a bit.
Of course, that's just conjecture on my part but that's the first thing I'd be looking for.
First of all, you should use move_uploaded_file instead of a copy.
But most likely your problem is due to a permissions problem. Can you upload any file to the folder? Have you checked that the files already in the folder (those you want to overwrite) have the same permissions as a newly uploaded one? My guess would be that you copied the files to the folder using (S)FTP and that they have a different owner/permissions so you can't overwrite them.
In addition to the possible permissions issues mentioned by paxdiablo and wimvds, also check the case of your filename & path. Unlike windows, unix filesystems are case-sensitive.

Categories