I was surprised PHP's filesize() fails on absolute paths??
My files are on my own server, how can I get the filesize except from converting them to relative (a mess)
EDIT
example:
$filename = 'http://172.16.xx.x/app/albums/002140/tn/020.jpg';
echo $filename . ': ' . filesize($filename) . ' bytes';
Warning: filesize() [function.filesize]: stat failed for http://172.16.xx.x/app/albums/002140/tn/020.jpg in /Applications/XAMPP/xamppfiles/htdocs/app/admin/+tests/filesize.php on line 26
END EDIT
I found this example for remote files:
$filename = 'http://www.google.com/logos/2010/stevenson10-hp.jpg';
$headers = get_headers($filename, 1);
echo $headers['Content-Length']; // size in bytes
Does this work without downloading the files?
http://172.16.xx.x/app/albums/002140/tn/020.jpg is not an absolute path, it is an URL. The absolute path for it would be something like /var/www/app/albums/002140/tn/020.jpg. You should use that absolute path in filesize().
filesize() supports only URL wrappers that support stat(). HTTP and HTTPS doesn't support that as mentioned in the manual page for HTTP and HTTPS wrappers.
Yes It will be work fine .
$filename = 'http://172.16.xx.x/app/albums/002140/tn/020.jpg';
$headers = get_headers($filename, 1);
$fsize = $headers['Content-Length'];
You can use like this ... for get file size by URL
$ch = curl_init('http://172.16.xx.x/app/albums/002140/tn/020.jpg');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
$data = curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
echo $size;
As I suspected, you're trying to give an HTTP URL to filesize(), which will not work. filesize() works on local filesystem URLs, such as those listed at http://www.php.net/manual/en/wrappers.file.php.
Presumably, as you're trying to access files on your own server, you must have the filesystem URL, rather than just an HTTP URL?
To use Function filesize();
you should have absolute path not the urls
http://www.php.net//manual/en/function.filesize.php
get_headers() will send a GET to your server, this add load to your web server.
I don't get it, your filesize() fails on absolute path ? It should not. According to php.net :
http://www.php.net/manual/en/wrappers.file.php
Edit:
I'm not sure about the error PHP will give you if allow_url_fopen is set to 0, but check this line in your PHP.ini (and restart apache then) : http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
If it's off, filesize() will not handle URL. Let me know if it was that.
I think you would have to use the filesize() function.
http://php.net/manual/en/function.filesize.php
echo filesize( $filename );
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 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);
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);
In my web hosting server, file_get_contents() function is disabled. I am looking for an alternative. please help
file_get_contents() pretty much does the following:
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
Since file_get_contents() is disabled, I'm pretty convinced the above won't work either though.
Depending on what you are trying to read, and in my experience hosts disable remote file reading usually, you might have other options. If you are trying to read remote files (over the network, ie http etc.) You could look into the cURL library functions
You can open the file with fopen, get the contents of the file and use them? And maybe cURL is usefull to you? http://php.net/manual/en/book.curl.php
A bit of everything.
function ff_get($f) {
if (!file_exists($f)) { return false; }
$result = #file_get_contents($f);
if ($result) { return $result; }
else {
$handle = #fopen($f, "r");
$contents = #fread($handle, #filesize($f));
#fclose($handle);
if ($contents) { return $contents; }
else if (!function_exists('curl_init')) { return false; }
else {
$ch = #curl_init();
#curl_setopt($ch, CURLOPT_URL, $f);
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = #curl_exec($ch);
#curl_close($ch);
if ($output) { return $output; }
else { return false; }}}}
The most obvious reason why file_get_contents() is disabled is because it loads the whole file in main memory first. The code from code_burgar could pose problems if your hoster has assigned you a very low memory limit.
As a general rule, use file_get_contents()(or -replacement) only when you are sure the file to be loaded is small. With SplFileObject you can walk trough a file line-by-line with a convenient interface. Use this in case your file is big.
Try this code:
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$content = curl_exec($ch);
curl_close($ch);
I assume you are trying to access a file remotely through http:// or ftp://.
In theory, there are alternatives like fread() and, if all else fails, fsockopen().
But if the provider is any good at what they do, those will be disabled too.
Use the PEAR package Compat. It is like a official replacement of native PHP functions with PHP coded solutions.
require_once 'PHP/Compat.php';
PHP_Compat::loadFunction('file_get_contents');
Or, if you don't wish to use the class, you can load it manually.
require_once 'PHP/Compat/Function/file_put_contents.php';
All compat functions are wrapped by if(!function_exists()) so it is really fail save if your webhoster upgrades the server features later.
All functions can be used exactly as the same as the native PHP, also the related constants are available!
List of all available functions
If all you are trying to do is trigger a hit on a given url and don't need to read the output you can use curl() provided your web host has it enabled on your server.
The documentation here gives an example of calling a url using curl.
If all else fails, there's always cURL. There's a good chance it's installed.