php how download file from url and save it on server - php

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);
?>

Related

Issues with using file_get_contents and file_put_contents in PHP

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);

Using file_get_contents() php file do not get executed when placed in a directory only root?

Using file_get_contents($filePath) to get php file executed via Url
is working fine as long as the php file - $sassConfigName - is in the root
question:
if the file is in a directory, how can i use the file the same way in the url?
working example:
$sassConfigName = 'sassconfig.php'; // sassconfig.php is in the root
$filePath = 'http://bootstrap4.dev/'.$sassConfigName.'/main.scss';
file_get_contents($filePath);
Not working example:
$sassConfigName = './config/sassconfig.php'; // sassconfig.php is in directory config. But now wrong parameters in Url.
$filePath = 'http://bootstrap4.dev/'.$sassConfigName.'/main.scss';
file_get_contents($filePath);
Regards
<?php
$sassConfigName="/php/";
$filePath='http://www.w3schools.com'.$sassConfigName.'func_filesystem_file_get_contents.asp';
echo file_get_contents($filePath);
?>

wordpress remove uploaded file

I have created a simple script to upload file in my WordPress plugin using
wp_handle_upload
In database only link to this image is stored. I would like to delete this uploaded file when i delete the post which it is linked to, however using
unlink()
does not work due to link structure which looks like this:
http://localhost/wp-content/uploads/2016/10/image.jpg
Does Anyone know the way to remove "http://[ip]/" from path or any WordPress method to remove uploaded file
I would be grateful for help.
You can use get_home_path() to get the root directory. Then your code would be:
$url = 'http://localhost/wp-content/uploads/2016/10/image.jpg';
$path = parse_url($url, PHP_URL_PATH); // Remove "http://localhost"
$fullPath = get_home_path() . $path;
unlink($fullPath);

move_uploaded_file() is not working but file is uploaded?

So i'm currently trying to create a code which simply creates and publishes a file to my webroot, modifies and writes to that file, and then finally change the location of the file to another directory/folder using move_uploaded_file()
This is my code so far
$myfile = fopen($_POST['title'].".txt", "w");
move_uploaded_file($myfile,'$dir/$title.txt');
fwrite($myfile, $_POST['textarea11']);
fclose($myfile);
The code doesn't work, i've tried echoing move_uploaded_file() and it returned nothing, however the file was uploaded but it's location just wasn't changed.
$dir is defined as $dir = __DIR__.'/../uploads/'; and $title is define as $title = $_POST['title'];
move_uploaded_file() can only be used if you are submitting a multipart form and you want to save the uploaded file.
What you probably need is this:
http://php.net/manual/en/function.rename.php
Change your given code as
$dir = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'uploads';
$myfile = fopen($_POST['title'].".txt", "w");
move_uploaded_file($myfile,"$dir".DIRECTORY_SEPARATOR."$title.txt");
fwrite($myfile, $_POST['textarea11']);
In your code
move_uploaded_file($myfile,'$dir/$title.txt');
php variable $dir and $title value is not coming. and value of $dir is consisting '/' and you are adding one more to make full file path too.
Always use directory separator to run in all Operating System. some OS use '/' and some OS use '\'.

(Used to work!) Get Facebook Picture and Copy To Server

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.

Categories