Is there any way to check if file has been uploaded completely on the server? My scenario: User uploads file over ftp and my other PHP task is running in cronjob. Now I would li
Check for the file after uploading by next code
if (file_exists('http://www.domain.com/images/'.$filename)) :
// Write some response as you like as a success message
endif;
Notice: you have to change images path by yours ,
good luck :)
if you are running php on linux then lsof can help you
$output = array();
exec("lsof | grep file/path/and/name.ext",$output);
if (count($output)) {
echo 'file in use';
} else {
echo 'file is ready';
}
EDIT: in case of permission issue. by using sudo or suid methods php script can get required permissions to execute lsof command. to set suid you have to issue following command as root.
su root
chmod u+s /usr/sbin/lsof
keep sql record of date modified and check if you have newely modified file.
<?php
$filename = 'somefile.txt';
if (file_exists($filename)) {
echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}
?>
<?php
if (move_uploaded_file($_FILES["uploader_name"]["tmp_name"], $path_name)):
//print true condition
else:
//print false condition
endif;
?>
If you have control over the application doing the upload, you can require that it upload the file to name.tmp
, and when it's done uploading rename it to name.final
. Your PHP script could look only for *.final
names.