File duplicates at upload with ftp_put() in php - php

So the situation is that lets say "client" changed their ftp server and I have a script that uploads stuff on their server via ftp_put(). I think they have different permissions now on their ftp server when I upload the files. Uploading behaves really weirdly with their new server, it duplicates some files about 5 times, others it doesn't duplicate at all. Keep in mind the same script worked correctly on their old server.
I noticed from the screenshot they sent me, that there's only read and write permissions on their server now. It used to have delete permission also. When I tried to upload files to our own server, and took away deleting permissions, everything stopped working.
So my thought is that, this is the issue maybe since logs showed, I might be totally wrong here, ftp_put() tries to delete files before it uploads them to a server to avoid duplicates. Is that correct and is there workaround to that?
The function itself what sends the files is really easy:
function send_to_ftp($ftp_server, $ftp_username, $ftp_password, $fileName,
$subFolder = "n/") {
$ftp_conn = ftp_connect($ftp_server);
if (!$ftp_conn) return false;
$login = ftp_login($ftp_conn, $ftp_username, $ftp_password);
$reciver_path = $subFolder . end(explode('/', $fileName));
$local_folder = $fileName;
return (ftp_put($ftp_conn, $reciver_path, $local_folder, FTP_ASCII));
//ftp_close($ftp_conn);
}

It is missing in the documentation on php.net but is mentioned in a comment.
ftp_put tries to overwrite the existing file, and this is why you have issues with the permissions. The doubleing might be, without reading the source code of the php ftp_put function, that the overwrite really is a delete and upload, or that you are running the function multiple times.
I recommend checking if the file already exists before trying to upload it (regardless, checking files are less costly in network traffic then uploading a full sized file).
Use ftp_nlist to get the content in the target path and see if it exists.
http://php.net/manual/en/function.ftp-nlist.php
If you need to upload already existing file you need to look over your permissions with the owner of the server.

Related

Uploaded files are neither moving into a new location , nor storing in database.

I have been tried many times to move the uploaded image or file in a location and also store the name in database.
All the functions work perfectly in LocalHost, but not on Server.
Neither I can see any thing in the directory of a server not any info of the uploaded file in a database.
I have googled and tried almost every trick to solve the issue. Common solution is to edit the permission of a folder. I did it too but all in vain.
What can be the solution ? What am I doing wrong ? The following code works perfectly if using Local host .
$destination_path ="uploads/";
$target = $destination_path.basename( $_FILES["Upload"]["name"]);
$filename=$_FILES['Upload']['name'];
$filetype=$_FILES['Upload']['type'];
$filetmp=$_FILES['Upload']['tmp_name'];
$images=move_uploaded_file($filetmp, $target);
$attachment = file_get_contents($_FILES["Upload"]["tmp_name"]);
$ins_sql="INSERT INTO wp_reklamation(Vorname,Nachname,Auftragsnummer,name,Upload,Grund,Wunsch,Telefonnummer,Email) VALUES('$vorname','$nachname','$auftrag','$filename','$images','$grund','$wunsch','$telefon','$email')";
Moreover, to send the attachment is also an issue. But I believe if this problem first can get resolved, I can solve that mail problem by using the above parameters . But Ofcourse nothing is selected nor moved in the location and database, then how can mail send the item name or anything else related to it .
Thanks
If the code works fine on localhost but not on your server, the most likely cause of the issue is that PHP file uploads are not enabled on your server.
To enable them, find the "file_uploads" directive in your 'php.ini' file and set it to on:
file_uploads = On
Also make sure that the user running your PHP server has permission to write to the target directory.
This question suggests creating a new group with permissions to read and write to the directory and adding the users for the PHP server and FTP as members, so the directory is still accessible through FTP and the server can write uploaded files to it.

Changing PHP Temp Directory

I have a site for media conversion where users can upload video, audio or image files and it is converted in to 3 popular formats. Conversion script works fine but I am having some issues with the tmp directory where the files get uploaded to. I have tried 2 scenarios I am fine with either but neither works and it seems to me to be permissions related but I can seem to fix the problem.
I am working locally right now while I work out the kinks.
Scenario 1:
File is uploaded to default local tmp directory (C:\WINDOWS\tmp) - works fine
Attempt to run conversion script using tmp file that was uploaded and it doesn't work - run from command line works perfectly fine though
Scenario 2:
File is uploaded to directory I have given IIS_IUSRS full control of (for testing) and file won't upload - yes the directory is spelt correctly (I changed the upload_tmp_dir value in php.ini)
Both the site the javascript that send the XMLHttpRequest to the PHP file, as well as the site the PHP file itself reside on are IIS sites so I assume the script is being run as IIS_IUSRS.
EDIT: Temp file is no longer being created at all for Scenario 1, can't figure out why I am assuming playing with permission messed something up because the code hasn't changed. I've given Modify to IIS_USRS and USERS to try and get it working again but no luck :( although the error log is still writing to the same folder...weird
NOTE: The "tmp_name" value of the $_FILES variable I am sending still has a value of "C:\WINDOWS\Temp\'filename'" but the file is not there
EDIT: Another new development, it appears it is NOT a permissions issue because I can create a temp file via $temp_file = tempnam(sys_get_temp_dir(), 'Test'); however it obviously does not contain the uploaded data so it does not solve my problem
PHP is ignoring the upload_tmp_dir because of one setting on APPLICATION POOLS.
It's not php-cgi.exe nor php.ini or a permissions issue.
Go to the application pool of the website experiencing the issue:
1. right click
2. select advanced settings
3. scroll to LOAD USER PROFILE and set it to FALSE.
that did the trick for me.
This is less of a problem solved and more of a workaround. The issue seems to be something with the
$_FILES['file']['tmp_name'];
When I echo the contents it looks as I expect however no file appears. So rather than taking advantage of that process that happens naturally, I have had to take a manual approach. What I have done is the following:
create a temp file
$temp_file = tempnam(ini_get('upload_tmp_dir'), 'php');
then add the content from the temp file created during the $_POST request. (which some how are in the $_FILES variable even though the file is not when I look in the directory)
file_put_contents($temp_file, file_get_contents($_FILES['file']['tmp_name']));
the I have my temporary file for processing.

Question About move_uploaded_file()

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.

Check if NFS share is up in PHP

I am working on a system that will store uploaded files. The metadata will go into a locally-accessible database, but the files themselves are going to be stored on a remote box via NFS so that PHP can interact with the server as if it was a directory.
I identified an issue that may occur if somebody attempts to upload a file when the NFS server is down or otherwise unavailable, which could cause the script to error out or hang. Obviously we want to avoid this scenario and handle it in a graceful manner, but we aren't sure how we can do this.
We are thinking of a) checking the server at page-display time and ghosting out the file upload portion of the form should the server be down, or b) checking the link before executing move_uploaded_file to store the uploaded document.
Is it possible to do this from within PHP, and if so, how?
Checkout http://www.php.net/manual/en/function.stream-set-timeout.php
You could write a simple check that tries to write to the NFS share with a 2 second timeout. If it succeeds, proceed with the move_uploaded_file. If it fails, give the user a graceful error.
I don't know what your setup looks like... If you are mounting it, could you use is_writable?
if (!is_writable('/path/to/nfs/share/mount')) {
die('NFS share is not writable!');
}
I'd try to write a small file for real at nfs-mountpoint, if success you're online and can write the posted file.
If not, cache it at webserver-disk for later (automatic) save.
Check if you can opendir() the directory?
<?php
$dir = "/etc/php5/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
// do your stuff
closedir($dh);
}
}
?>

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