I have to download an image in a weblink to my local folder using php. Below is the program.
<?php
if (isset ($_POST['submit'])){
$URL = 'http://10.251.13.7/gtz/temp3.php/download.png'; // Like "http:// ...."
$FileToSave = 'uploads/download.png'; // Like "/home/.." or "C:/..."
$Content = file_get_contents($URL);
file_put_contents($FileToSave, $Content); //file_put_contents('uploads/image.jpg',file_get_contents('10.251.13.7/gtz/temp3.php/mtps_files.png'));
} ?>
I could successfully download the image. But when I try to open it's showing the file has been damaged. In one way I am able to right click the image in the weblink and save the file to my local directory. It works.
On the other hand, in php programming I was able to download the file and when I access the file, it is showing as file got damaged.
Please review below code. Please assign the home folder path in file & save file.
$URL = 'C:/home/demo/Downloads.png'; // Like "http:// ...."
$FileToSave = 'D:/demo/download.png'; // Like "/home/.." or "C:/..."
$Content = file_get_contents($URL);
file_put_contents($FileToSave, $Content);
Related
my link (URL) is different!!! and does not work with usual method
I think because site load with js or aspx
you can test my link (URL) in your browser and see download starting
but cant work in php
I have tested all methods (fetch, curl, file, get, put), but it does not work.
I have a similar URL here: 'http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0'
I can open it in the browser and download a csv file I need to do this in php and save the csv file on server
Here is what I have tried so far:
<?php
$file = fopen('http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0');
$file = file_get_contents('http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0');
file_put_contents('ClientTypeAll.csv', $file);
?>
I do not want Contents !!! I want a csv file form my link
if you test my link in your browser download start in your pc
I run this code with a remote PDF file.
<?php
$url = 'https://example.com/file.pdf';
$dir_name = 'storage-x'; // saVe the directory name
if (!file_exists($dir_name)) {
mkdir($dir_name, 0777, true);
}
$path = $dir_name.'/'.rand().'.pdf';
$file = file_get_contents($url);
file_put_contents($path, $file);
?>
Please follow the below step.
Get file form URL.
Set directory and check file exit condition and update directory access permission.
Set new file path, name, and directory.
Save the file.
Please check the below example which works for me.
<?php
$file = file_get_contents('https://example.com/file.pdf');
$dirName = 'storage-pdf';
if (!file_exists($dirName)) {
mkdir($dirName, 0777, true);
}
$newFilePath = $dirName.'/'.rand().'.pdf';
file_put_contents($newFilePath, $file);
?>
i was trying to copy some images from url and when i encode that url directly in php copy function it copies without any issue but when i get that as
image url https://dl.iteens.ir/wp-content/uploads/2017/12/wedding-cake-topper-many-layers-wedding-dress-clay-by-asiaworld-120-242x300.jpg
$decoded = json_decode($content, true);
$urls = $decoded['url'];
and pass $urls in copy it does not work.
copy($urls,'path/img.jpg');
I have tested the same with
$myfile = fopen("/something/newfile.txt", "w")
fwrite($myfile, $urls);
fclose($myfile);
i can see exact same url written in newfile.txt
but this work
copy('https://dl.iteens.ir/wp-content/uploads/2017/12/wedding-cake-topper-many-layers-wedding-dress-clay-by-asiaworld-120-242x300.jpg','path/img.jpg');
i tried different combination of copy like
copy("$urls","path/img.jpg");
nothing worked for most of the images the copy($urls,'path/img.jpg'); works but not for this link. i dont think its ip block from either server because when we give direct link it works
i further tried by adding fgets($myfile); and tried to copy same thing from text file and it worked so dont know what is the problem
I've created a php file that creates another php file,
heres the code:
if (isset($_POST["Submit"])) {
$name=$_POST['name'];
$string = '<?php
echo "'. $_POST["message"]. '";
?>
';
$fp = fopen("$name.php", "w");
fwrite($fp, $string);
fclose($fp);
}
it successfully created a php file using xampp in my local machine,
but when i upload it to my webhosting site, everytime i click submit, it doesnt create any file i written.
I dont get what is wrong with it,
somebody said that i need to connect it to my ftp server,
but i dont get it.
Advance thanks guys for the help.
Check your rigths on a dir where you want to save the file. It must be 664.
I think you have 3 things to solve here.
1) File permission
2) Rewriting a file
3) I thing there is an error on your line with $string because you are already in a php code
Try this
<?php
//If the form is submited
if (isset($_POST["Submit"]))
{
//we parse the informations
$name=addslashes($_POST['name']);
$string = addslashes($_POST["message"]);
//We will recreate the file even if it was existing
$fp = fopen("$name.php", "w+");
fwrite($fp, $string);
fclose($fp);
//We give access permission to the file
chmod("$name.php", 0777);
}
?>
And also if you are creating the file in a directory, use your ftp software like FileZilla to connect to your ftp and right click on the directory and click on Chmod. Make a Chmod 0777 on that directory. And everything should work fine.
Submit verification
Now if still it does not work, you can search bit by bit for the error by creating a file name test.php with this code
<?php
//we parse the informations
$name='my_one_file';
$string = '<h1>It is working<h1>';
//We will recreate the file even if it was existing
$fp = fopen("$name.php", "w+");
fwrite($fp, $string);
fclose($fp);
//We give access permission to the file
chmod("$name.php", 0777);
echo "<a href='$name.php' target='_blank'> <h1> $name.php</h1> </a>";
?>
And then you launch yourwebsite/test.php in your browser. If it works you should have a file name my_one_file.php on your server. If it shows a link then the problem is with your if (isset($_POST["Submit"])). Meaning we have to cross check your html form.
Touch Verification
Finaly if it is still not working then we can verify if the file needs to be created first on your server before being modified like some server do.
For that you will still create a test.php file with the following code
<?php
$name='my_one_file';
$string = 'My message';
touch("$name.php");
//We will recreate the file even if it was existing
$fp = fopen("$name.php", "w+");
fwrite($fp, $string);
fclose($fp);
//We give access permission to the file
chmod("$name.php", 0777);
echo "<a href='$name.php' target='_blank'> <h1> $name.php</h1> </a>";
?>
And then lauch yourwebsite/test.php. If it works you should have a file name my_one_file.php on your server.
I'm trying to get a few PDF files I need for a project from a website (legally), but am running into some issues.
The URL of the location of the PDF file is f.e. example.com/?download_id=290758&s=d12134cac7ddb2198d232bba75c07d57&t=2-1-2014%2016:19:00
So first of all I parse this URL from the page containing it with the following code:
$html = file_get_html('http://www.example.com/Acer-CR-6530/manual-5-100076.html');
// find the download link containing the session ID
foreach($html->find('a[rel=nofollow]') as $e)
$link = $e->href;
$link = $baseURL . $link;
// Link: example.com/?download_id=290758&s=d12134cac7ddb2198d232bba75c07d57&t=2-1-2014%2016:19:00
echo file_get_contents($link);
This doesn't work. It doesn't output the PDF file. Should I use cURL? If so, I'm not a cURL pro so I would love to see code for doing this.
Thanks in advance!
I have this code in PHP that used to work but now it isn't working.
I'm trying to grab the user's profile picture from Facebook, then copy and paste it into a directory on my server.
$url = 'http://graph.facebook.com/'.$fb_userID.'/picture?type=large';
$data = file_get_contents($url);
$fileName = 'users/avatars/'.$fb_username.$fb_userID.'.jpg';
$file = fopen($fileName, 'w+');
fputs($file, $data);
fclose($file);
Update: This code will put a jpg image into my directory with a size of 0 Kb so I'm pretty sure the content is never being grabbed correctly. Please help!
The solution was to adjust something in my php.ini file (or php5.ini) on my server.
I had to change this:
allow_url_fopen = Off
To on:
allow_url_fopen = on
Done.