Rename and saving remote image to server - php

i'm looking for a simple way to download remote images to my sever, rename and save image name to database.
I know how to download the file easily to my sever
copy('http://example.com/file.jpeg', '/tmp/file.jpeg');
But how to i rename the image while downloading or after download?
I would really appropriate if anyone can tell me a simple way to do this using php

In copy function just change file name:
copy('http://example.com/file.jpeg', '/tmp/any_file_name.jpeg');

I think this should do the Job
rename("/tmp/tmp_file.jpg", "/home/user/login/docs/my_file.jpg");
you have to do this after you downloaded it
or just change the file name while you dwonload it e.g.
copy('http://example.com/file.jpeg', '/tmp/some_name.jpg');

Related

how to upload excel without browse option in php

i can easily upload excel if i used simple
<input type="file" name ="file">
this function simple browse the file and using simple php code excel data is saved in the data base. I need to upload excel file in some directory on time bases by cron job . i know very well cron job but. i am unable to do without input type file there is any way to upload file with using file type.
Pl z suggest me if any solution for that i waiting.
Thanks and regards
You can use wget or curl, see this thread on Superuser for examples:
https://superuser.com/questions/86043/linux-command-line-tool-for-uploading-files-over-http-as-multipart-form-data
The rename function does this (delete old file)
rename('text.xls', 'folder_2/text.xls');
PHP RENAME DOC
if you want to keep old file
The copy function
copy('text.xls', 'folder_2/text.xls');
PHP COPY DOC

Downloading pdf files from a website and changing the filename after download

I am trying to download some files. I have written a php script that can gather all of the address of the pdf files I need to download into a array but the file names on that server are odd. How does one go about downloading pdf files with php?
you can try (if I understand your question correctly)
file_put_contents('/your/file/name.pdf',
file_get_contents('http://www.example.com/oddFile.pdf'))
Check out the file or file_get_contents functions. It can take URLs as well as local filenames.
http://www.php.net/manual/en/function.file.php
http://www.php.net/manual/en/function.file-get-contents.php

adding remote files to a zip file

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.

Uploading images from server using Php

In my php application i have a folder in which all the photos are kept.
The images are in different sizes.So i want to select photos from the folder and applying some image functions and upload to a different folder through php code.
This is same as image uploading but the difference is that the source file is in server
That is i want to select photos from server applying some image functions and upload again on the server
Pls help me
If you want to keep them on the same server, no further uploading is necessary. You can just perform whatever manipulations you want (resize, etc.) then use PHP's filesystem libraries to move the files around on the server. Check them out here.
maybe with curl http://www.askapache.com/htaccess/sending-post-form-data-with-php-curl.html here a tip explaining how to send data with curl
If you select a photo from the folder and applied some image functions, instead of saving the file back to the original file, you could simply save it to it's new location...
In that case you don't need uploading and/or moving...

'pass through' php upload to amazon's s3?

I've seen ways to upload files directly to S3 and I've seen ways to upload a file already on the server. I need to modify the file's name before sending it on to S3, but I don't want to store it locally and then upload it, if I can help it. Is there a way to put it in a buffer or something? Thx.
The file will always end up in the temporary directory first while the upload completes, even before you're able to work with the uploaded file. You get all the file's chunks before, and then it get rebuilt in the /tmp directory by default. So no, there's no "pass-through". But I guess you could re-upload directly from the temporary directory afterwards instead of moving it to another working directory.

Categories