So a PHP program I built to download files from one server and upload them to another is now needed to rename the files.
For some reason I'm getting a 'invalid argument' warning on the rename() function which seems impossible because the file exists and there is 0 white space.
So now I was wondering if it was possible to rename a file as I download it from an ftp using ftp_get()?
http://php.net/manual/en/function.ftp-get.php
Would suggest so. Give it a local file name.
Also make sure you are using rename() correctly
http://uk.php.net/manual/en/function.rename.php
Related
On my server, I'm simply unable to use move_uploaded_files function in PHP, it gives me a permission denied warning. I get the same warning when trying to delete files. I managed to alter the delete script using FTP function ftp_delete and ftp_rmdir.
So now I'm wondering, if it is safe to use ftp_put for file upload ?
If you are sure that the file you are moving is the file uploaded by the user, then yes.
To be on the safe side, it is good to have writable temp folder, where you can move uploaded file, process it if needed, upload with ftp to proper place with proper permissions, and delete from temp folder.
I am trying to upload a file via HTTP and run it through a virus scan and immediately move to a ftp location. Here are the basic steps,
Upload via HTTP
Do the virus scan on the php tmp directory (upload_tmp_dir) using php command line functions.
Move the file directly to ftp from the tmp directory after the virus check.
You might notice I am not doing a move_upload_file(), is this the best way to do it ? Or should I do steps 2 & 3 after I do a move_upload_file().
If I should not do it before move_upload_file() then what would be the reason ?
Thanks
I think that doing it before move_upload_file is appropriate. If you move it first, then you've put a file into your FTP directory which may have a virus; for the length of time that you scan, an unsafe file is available.
Also, if you do it before move_upload_file and find a virus, you can just leave it there and PHP will tidy up for you; no need to call unlink yourself :)
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
Is there a way to add files to a zip file from another server with php's zip extension? ie.
addFile(array('localfile.txt,'http://www.domain.com/remotefile.txt'))
//(that obviously does not work)
I suppose I can download the files to a temporal folder and then add them to the zip file, but I was looking for a more automated solution or a function already made
use file_get_contents() and ZipArchive::addFromString()
$zipArchiveInstance->addFromString($filename, file_get_contents($mediaUrl));
This writes the contents fetched remotely straight into your php object (no need to write/read temp file)
It's not hard to read remote files in PHP.
file_get_contents("http://example.com/remote.txt");
Or to copy them locally:
copy("http://example.com/remote.txt", "/tmp/local.txt");
Whichever way you do it, the contents are going to have to be transferred to a local temp folder or memory before you can do anything with them.
Fetch them with cURL, add them from TEMP directory.
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.