I am able to save images from a website using curl like so:
//$fullpath = "/images/".basename($img);
$fullpath = basename($img);
$ch = curl_init($img);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$rawData = curl_exec($ch);
curl_close($ch);
if(file_exists($fullpath)) {
unlink($fullpath);
}
$fp = fopen($fullpath, 'w+');
fwrite($fp, $rawData);
fclose($fp);
However, this will only save the image on the same folder in which I have the php file that executes the save function is in. I'd like to save the images to a specific folder. I've tried using $fullpath = "/images/".basename($img); (the commented out first line of my function) but this results to an error:
failed to open stream: No such file or directory
So my question is, how can I save the file on a specific folder in my project?
Another question I have is, how can I change the filename of the image I save on the my folder? For example, I'd like to add the prefix siteimg_ to the image's filename. How do I implement this?
Update: I have managed to solve first problem with the path after trying to play around with the code a bit more. Instead of using $fullpath = "/images/".basename($img), I added a variable right before fopen and added it to the fopen method like so:
$path = "./images/";
$fp = fopen($path.$fullpath, 'w+');
Strangely that worked. So now I'm down to one problem which would be renaming the file. Any suggestions?
File paths in PHP are server paths. I doubt you have a /images folder on your server.
Try constructing a relative path from the current PHP file, eg, assuming there is an images folder in the same directory as your PHP script...
$path = __DIR__ . '/images/' . basename($img);
Also, why don't you try this much simpler script
$dest = __DIR__ . '/images/' . basename($img);
copy($img, $dest);
Related
So I found this page: Load external XML and save it using PHP to help me out, but it doesn't seem to work for me.
I'm trying to do the same thing by loading an external xml file and saving it (with no changes to the xml file) into my website directories as a batch system. I have already dynamically created all the directories needed.
ex:
/xml/en/281
Now what I'm trying to do is load the company's xml files (https://thiscompany.com/xml/en/281/18511095_en.xml) and save it in my own directory as the same name, 18511095_en.xml in the 281 directory.
I've been researching and I am getting lots of simplexml_load_file and DOMDocument examples but I'm not getting the results I needed.
For all sake and purposes here is the code: (I'm changing the url of the actual xml file because my client doesn't want it out there.
EDITED from the responses below
$url = "https://thiscompany.com/xml/en/281/18511095_en.xml";
$timeout = 10;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
$response = curl_exec($curl);
file_put_contents(__DIR__ . "/xml/18511095_en.xml", $response);
I'm assuming the problem is with the saveXML path. The xml directory is at the root of my website. Do I need to include, http://www.....com?
xml, en, and 281 all have 0777 permissions.
Any help would be appreciated.
You need to provide the path on your filesystem, not a URL.
e.g. "/var/www/www.example.com/htdocs/xml/en/281/18511095_en.xml"
Here's a simple way to copy a remote file into the same directory as your PHP file:
$url = "http://www.test.com/xmlfile.xml";
$contents = file_get_contents($url);
file_put_contents(dirname(__FILE__) . "/xmlfile.xml", $contents);
You can also accomplish this without having to use simplexml_load_string which will use more memory on your server. Instead try the following:
$xml = file_get_contents("https://thiscompany.com/xml/en/281/18511095_en.xml");
file_put_contents("/var/www/my/full/data/path/filename.xml", $xml);
$xml = simplexml_load_string($response);
$xml->saveXML("/xml/en/281/18511095_en.xml");
First of all, you should use a relative path instead of an absolute one. In this example you are trying to save the file under the /xml folder on the root filesystem and there's most likely no such directory and you don't have permissions to write to that directory (assuming it exists). Use a relative path.
Secondly, you don't need to parse the XML file, you can save it directly. Here's a working example:
$response = curl_exec($curl);
file_put_contents(__DIR__ . "/xml/en/281/xmlfile.xml", $response);
Further reading: Absloute path vs relative path in Linux/Unix
I need PHP code to download file from one server to another. It's defined by variable $HasPrevod. That's the file which I need to download. Also there's simple form where I put link and the output is that variable $HasPrevod, so I had to put it in a href to get link easily to download.
Download. and download it manually and upload via Filezilla on my server. Is there solution which will download content from that variable and put it on my server in some folder. I tried with cURL but nothing happened because I'm newbie and I've made mistake for shure, and I don't know much about PHP and cURL.
So it should be something like this $HasPrevod file download -> my server / folder, and echo file name.
$prevod = $_POST['prevod'];
$url = file_get_contents("$prevod");
$PrevodLink = preg_match('!http://[a-z0-9\S\ \-\_\.\[\]\[\]\/]+\.(?:srt)!Ui', $url, $match1);
$HasPrevod = $match1['0'];
<form action="prevod.php" method="post">
<input name="prevod" type="text"/>
<input type="submit" value="Search"/>
</form>
Thats the code I'm using to put link and get path to the file which is located on another website not my own, then I have to download manually and upload on my server via Filezilla.
Download
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
$destination = "prevodi/". $HasPrevod;
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
file_put_contents('path/to/file.ext', file_get_contents($HasPrevod));
If you have enough permission, try with exec('wget http://www.example.com/myFile.zip');
If not, try with file_put_contents() method. At last if that now works, try to var_dump() some states of code to debug it.
I'm downloading and saving a file from another server to my server, except the file I'm downloading comes attached with an access token.
http://www.example.com/video.mp4?versionId=c_.Qeh.dz.zqPA3zc57HFDKEAmKG3xr2
Loading the following results in a permission error:
http://www.example.com/video.mp4
Problem is, when I cURL with the following code:
$url = 'http://www.example.com/video.mp4?versionId=c_.Qeh.dz.zqPA3zc57HFDKEAmKG3xr2';
$fh = fopen(basename($url), "wb");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_exec($ch);
curl_close($ch);
The file saves as video.mp4?versionId=c_.Qeh.dz.zqPA3zc57HFDKEAmKG3xr2 (with token) and not video.mp4, which means I can't do anything with it afterwards as it's not an .mp4
What's the solution here? I tried
rename(video.mp4?versionId=c_.Qeh.dz.zqPA3zc57HFDKEAmKG3xr2, video.mp4)
but it requires filenames and the access token is preventing that.
Try to use parse_url instead of basename or combine them. Take path from parse_url (without GET params) and then use basename function.
http://www.php.net/manual/en/function.parse-url.php
try basename( parse_url( $url, PHP_URL_PATH ) )
I'm having trouble downloading a remote file via PHP.
I've tried using cURL and streaming, neither of which produces an error.
Here's my current code for streaming.
$url = "http://commissiongeek.com/files/text.txt";
$path = "/files/cb.txt";
file_put_contents($path, file_get_contents($url));
I'll be downloading a zip file when I get this working, but in theory this should work just fine...
The folder's permissions are set to 777, and as said before, no errors are being thrown.
What could cause this?
Split this up into multiple sections, so you can verify that each stage is working:
$url = 'http://...';
$txt = file_get_contents($url);
var_dump($txt);
var_dump(file_put_contents('/files/cb.txt', $txt));
The first dump SHOULD show you whatever that text that url returns. The second dump should output a boolean true/false depending on if the file_put failed or not.
It seems you have an absolute path that you are trying to save in. I believe you want the path changed to "files/cb.txt" instead and do not have any access to /files/
If you have allow_url_fopen set to true:
$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
Else use cURL:
$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
I'm writing tests for my CodeIgniter site using simpletester.
One of the things that it does is offer autogenerated files.
I want to test if the output of a file is correct.
However, if I do this:
function testFunction1(){
$url = site_url('downloader/function1');
$handle = fopen($url,'r');
$contents = stream_get_contents($handle);
echo $contents;
fclose($handle);
}
It outputs a 404.
It works perfectly when I put "www.google.com" as the url.
I can also download the file if I copy paste the URL in the browser.
Thanks!
edit:
Even if I try to download a regular file (so not a php function), with the full url, it gives the 404.
when your using relative paths, simply remove site_url() for relative path instances.
fopen accepts the following:
$handle = fopen("/home/rasmus/file.txt", "r");
$handle = fopen("/home/rasmus/file.gif", "wb");
$handle = fopen("http://www.example.com/", "r");
$handle = fopen("ftp://user:password#example.com/somefile.txt", "w");
But site_url seems to be adding something.
also be sure to check the relative path directory is corrent.
If you're accessing locally then just use
$url = "main/function":
$file_content = file_get_contents("http://localhost/codeignitor/" . $url);