Hello how do I return the filename with extension using file_get_contents
here is my code :
$lien ="http://www.miawmiaw.com/coolimage.jpg";
$data = file_get_contents($lien);
$fichier = basename($lien);
$fp = fopen("products/".$fichier,"wb");
if (!$fp) exit;
fwrite($fp, $data);
fclose($fp);
so here $fichier should have the file name such as "coolimage.jpg"
you have used $path inside basename($path); instead of this just pass $lien inside the method
$lien ="http://www.miawmiaw.com/coolimage.jpg";
$data = file_get_contents($lien);
$fichier = basename($lien);
$fp = fopen("products/".$fichier,"wb");
if (!$fp) exit;
fwrite($fp, $data);
fclose($fp);
Just use basename()
$filename = basename("http://www.miawmiaw.com/coolimage.jpg");
Example: http://codepad.org/4ajeBuig
Related
On the 000webhost.com server
This is the code
<?php
$my_file = fopen("my_test.txt", 'w');
fclose($my_file );
I thank those who help me in advance
Use the function chmod
http://it2.php.net/function.chmod
$file = "my_test.txt";
$fp = fopen($file, 'w');
fwrite($fp, $content);
fclose($fp);
chmod($file, 0777);
Note* fwrite is optional in your case if you need to enter some data in file
PHP script who open and search data from .txt is:
function explodeRows($data) {
$rowsArr = explode("\n", $data);
return $rowsArr;
}
function explodeTabs($singleLine) {
$tabsArr = explode("\t", $singleLine);
return $tabsArr;
}
$filename = "/txt/name.txt";
$handle = fopen($filename, 'r');
$data = fread($handle, filesize($filename));
$rowsArr = explodeRows($data);
for($i=0;$i<count($rowsArr);$i++) {
$lineDetails = explode("|",$rowsArr[$i]);
if ($kodas == $lineDetails[2]) {
$link3=$lineDetails[4];
echo "";
} }
fclose($handle);
It's works well, but now I transfer name.txt to another folder (folder name txt). How to make, first open this folder and search open name.txt
$filename = "txt/name.txt";
$handle = fopen($filename, 'r');
$data = fread($handle, filesize($filename));
$rowsArr = explodeRows($data);
Using php fwrite in a symfony2's controller to write a Base64 image, the file not always has been write in the directory.
The file that i write has every time a different name,so there aren't problem for the
rewrite of the same file.
The variable $data has already been decoded.
public function convertAction(Request $request){
$data= $request->request->get('data');
$filen= $request->request->get('filename');
$uploadDir = $this->container->getParameter('upload_tmp');
$file = $uploadDir . $filen;
$fp = fopen($file, 'wb');
fwrite($fp, $data);
fclose($fp);
return new Response() ;
}
fopen and fwrite return false in case of an error. Do you get false from one of these functions when no file is written?
You can try:
public function convertAction(Request $request){
$data= $request->request->get('data');
$filen= $request->request->get('filename');
$uploadDir = $this->container->getParameter('upload_tmp');
$file = $uploadDir . $filen;
$fp = fopen($file, 'w') or die ('Something went wrong'); // That show is an error ocurres The flag for read/write in binary secure mode "wb" does nothing with fopen()
fwrite($fp, $data);
fclose($fp);
return new Response() ;
}
Whit the follow code:
$file = fopen('php://input', 'r');
$temp = tmpfile();
$imageSize = stream_copy_to_stream($file, $temp);
$imageDimensions = getimagesize($file); // Error here
$imageInfos = pathinfo($_GET['selected-image']);
I get this error
getimagesize() expects parameter 1 to be string, resource given
Because $file is a resource of fopen. How I can have a resource and a string if I can't read php://input twice?
UPDATE
I have tried this:
$file = fopen('php://input', 'r');
$tempName = tempnam(sys_get_temp_dir(), '.upload');
$imageSize = fwrite(fopen($tempName, 'w+'), stream_get_contents($file));
$imageDimensions = getimagesize($tempName);
$imageInfos = pathinfo($_GET['selected-image']);
// Unlinks and other stuffs
The error:
Notice: getimagesize() [function.getimagesize]: Read error! in
Thanks!
Because getimagesize() only accepts string filenames, not resources, you will need to use tempnam() to create a temporary file with a name that you can pass to getimagesize().
$file = fopen('php://input', 'r');
$tempName = tempnam('/var/tmp', 'img_');
$temp = fopen($tempName, 'w');
$imageSize = stream_copy_to_stream($file, $temp);
fclose($temp);
$imageDimensions = getimagesize($tempName);
This assumes that the user running PHP has write and read permissions to the directory /var/tmp, but if it doesn't you can change to a directory that the user does have r/w permissions to.
Use tempnam() to generate your temp file, instead of tmpfile(). Then you have a file path which you can pass to getimagesize():
$file = fopen('php://input', 'r');
$tempname = tempnam('.', 'img');
$tempfile = fopen($tempname, 'w+');
$imageSize = stream_copy_to_stream($file, $tempfile);
$imageDimensions = getimagesize($tempname);
$imageInfos = pathinfo($_GET['selected-image']);
// At the end of the script, you need to remember to...
fclose($tempfile);
unlink($tempname);
Trying to download a file on a remote server and save it to a local subdirectory.
The following code seems to work for small files, < 1MB, but larger files just time out and don't even begin to download.
<?php
$source = "http://someurl.com/afile.zip";
$destination = "/asubfolder/afile.zip";
$data = file_get_contents($source);
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
?>
Any suggestions on how to download larger files without interruption?
$ch = curl_init();
$source = "http://someurl.com/afile.zip";
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
$destination = "/asubfolder/afile.zip";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
file_get_contents shouldn't be used for big binary files because you can easily hit PHP's memory limit. I would exec() wget by telling it the URL and the desired output filename:
exec("wget $url -O $filename");
Since PHP 5.1.0, file_put_contents() supports writing piece-by-piece by passing a stream-handle as the $data parameter:
file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
I always use this code,it's working very well.
<?php
define('BUFSIZ', 4095);
$url = 'Type The URL Of The File';
$rfile = fopen($url, 'r');
$lfile = fopen(basename($url), 'w');
while(!feof($rfile))
fwrite($lfile, fread($rfile, BUFSIZ), BUFSIZ);
fclose($rfile);
fclose($lfile);
?>
Use this solution if you do not know the format of the file that you are going to download.
$url = 'http:://www.sth.com/some_name.format' ;
$parse_url = parse_url($url) ;
$path_info = pathinfo($parse_url['path']) ;
$file_extension = $path_info['extension'] ;
$save_path = 'any/local/path/' ;
$file_name = 'name' . "." . $file_extension ;
file_put_contents($save_path . $file_name , fopen($url, 'r'))
Try out phpRFT:http://sourceforge.net/projects/phprft/files/latest/download?source=navbar
It have progress_bar and simple filename detactor...
A better and lighter script which is streaming file:
<?php
$url = 'http://example.com/file.zip'; //Source absolute URL
$path = 'file.zip'; //Patch & file name to save in destination (currently beside of PHP script file)
$fp = fopen($path, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);
?>