I need help with this http file transfer from http to folder

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

I need help with this http file transfer from http to folder

Post by danjapro »

Here is my code, below. It is gathering the $url with the corret images. But poduces and error on line 50. Failed to open stream. No such file or directory.

By this line: readfile($file_name);

What Am I missing, beyond strong programmer mind set.

Code: Select all

$merch_map = "http://spot_map"; 
$dir = "/opt/tmp/media/current";


class merch_imgs {
	public function __construct() {
		$this->merch_imgs1();
	}
	public function __call($method, $_) {
		$count = str_replace('merch_imgs', '', $method);
		echo "$count ";
		$this->{"merch_imgs" . ++$count}();
		
	}
}
class thousand_printer extends merch_imgs {
	public function merch_imgs1000() {}
}



	//03-2012. Location of merchant, if no image use place holder
	$thumbnails = implode(',', range(1, 10000));
	$ext = ".png";
	for($thumbnails = 0; $thumbnails < 10000; $thumbnails++) { 
		$url = $merch_map.'/'.$thumbnails.''.$ext;   
	
		echo $url . '<br>';
	    //echo '<img src='.$url.' border=0/>'; 
	   downloader($url, $dir);
	}



function downloader($url){
    $file_name = basename($url);
    $generated_files = geturl($url, $url);
    //file_put_contents($file_name);
	file_put_contents($url, $url = var_export($file_name,true));
	$size=strlen($generated_files);
	if($size==0){exit(0);("<b>Error:</b>not found!");}
	//header('Content-type: application/force-download');
    //header('Content-Disposition: attachment; filename=' . $file_name);
    //header('Content-Transfer-Encoding: binary');
    //header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
    //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    //header('Pragma: public');
    //header('Content-Length: ' . $size);
    readfile($file_name);
    unlink($file_name);
}

function geturl($url, $referer) {
    $headers[] = 'Accept: image/png';
    $headers[] = 'Connection: Keep-Alive';
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
    $user_agent = 'ONLY_MY_HOST';
	//$useragent = $url;
    $process = curl_init($url);
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($process, CURLOPT_HEADER, 0);
    curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($process, CURLOPT_REFERER, $referer);
    curl_setopt($process, CURLOPT_TIMEOUT, 30);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
    $return = curl_exec($process);
    curl_close($process);
    return $return;
}







//downloader($dir, $url);



//Merchant Images
if($url && $user_agent){
	file_get_contents($url);
	die();
}
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: I need help with this http file transfer from http to fo

Post by califdon »

Could you tell us which is line 50?

Have you confirmed that your path to the files is correct?

Have you printed out the variables at the point where the error occurs?

This error says that it cannot find the file or the directory that it's being asked to find. Either the path is incorrect or there is no file there with that name. You must print out the values it is being asked to find, then check that against your server's file system.
danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

Re: I need help with this http file transfer from http to fo

Post by danjapro »

I am getting the files to download to the same directory the script live in, But I need to get them to download to the set $dir .

Code: Select all

$dir = "/opt/tmp/media/current";

//03-2012. Location of merchant, if no image use place holder
    $thumbnails = implode(',', range(1, 10000));
    $ext = ".png";
    //for($thumbnails = 0; $thumbnails < 10000; $thumbnails++) { 
for($thumbnails = 1; $thumbnails < 500; $thumbnails++) {
        $url = $merch_map.'/'.$thumbnails.''.$ext;   
    
        echo $url . '<br>';
        //echo '<img src='.$url.' border=0/>'; 
       downloader($url, $dir);
    }



function downloader($url){
    $file_name = basename($url);
    $generated_files = geturl($url, $url);
    file_put_contents($file_name, $generated_files);
    $size=strlen($generated_files);
    if($size==0){exit(0);("<b>Error:</b>not found!");}
    //header('Content-type: application/force-download');
    //header('Content-Disposition: attachment; filename=' . $file_name);
    //header('Content-Transfer-Encoding: binary');
    //header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
    //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    //header('Pragma: public');
    //header('Content-Length: ' . $size);
    readfile($file_name);
    unlink($file_name);
}

function geturl($url, $referer) {
    $headers[] = 'Accept: image/png';
    $headers[] = 'Connection: Keep-Alive';
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
    $user_agent = 'ONLY_MY_HOST';
    //$useragent = $url;
    $process = curl_init($url);
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($process, CURLOPT_HEADER, 0);
    curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($process, CURLOPT_REFERER, $referer);
    curl_setopt($process, CURLOPT_TIMEOUT, 30);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
    $return = curl_exec($process);
    curl_close($process);
    return $return;
}

What I'm just missing something, it downloads all images, but not to correct directory.....
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: I need help with this http file transfer from http to fo

Post by Celauran »

Can you post the code for the downloader() function?
danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

Re: I need help with this http file transfer from http to fo

Post by danjapro »

I changed the code up some, I can now get images to download, but still does not download to the $dir, that I have set.

Code: Select all


function geturl($url, $referer) {
	$headers[] = 'Accept: image/png';
	$headers[] = 'Connection: Keep-Alive';
	$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
	$user_agent = 'ONLY_MY_HOST';
	$process = curl_init($url);
	curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($process, CURLOPT_HEADER, 0);
	curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
	curl_setopt($process, CURLOPT_REFERER, $referer);
	curl_setopt($process, CURLOPT_TIMEOUT, 30);
	curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
	$return = curl_exec($process);
	curl_close($process);
	return $return;
}

function downloader($url) {
	$file_name = basename($url);
	$generated_files = geturl($url, $url);

	file_put_contents($file_name, $generated_files);
	$size = strlen($generated_files);
	if ($size == 0) {
		echo '<b>Error:</b>not found!<br />';
	}
}

echo 'images be saved to ' . getcwd() . '<br />';

$merch_map = "http://tdr.aaa.com/tdr-images/images/mapping/dynamic/aaamemberdeals/spot_map"; 
$dir = "C:/wamp/www/merchant_batch/merchant_maps";

$ext = ".png";
for($i = 1; $i < 1000; ++$i) {
	$image = "{$i}{$ext}";
	$url = "$merch_map/$image";   
	echo "$url <br />";
	downloader($url, $dir);
	echo "<img src=\"$image\" /><br />";
}

Last edited by califdon on Fri Apr 06, 2012 12:23 pm, edited 1 time in total.
Reason: Moderator changed [code ] tags to [syntax=php ] tags for readability.
danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

Re: I need help with this http file transfer from http to fo

Post by danjapro »

I can get it to download, but only to directory where the script resides. I need it to download to a set directory $dir, can anyone please take alook and assist me please...
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: I need help with this http file transfer from http to fo

Post by califdon »

In the next-to-last line of your code, you are calling the downloader function with 2 arguments:
downloader($url, $dir);
but the declaration of the downloader function only recognizes one argument:
function downloader($url)
so the $dir value is ignored. You will have to write the downloader function so that it requires 2 arguments and then uses the second argument to determine where to save the file.
danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

Re: I need help with this http file transfer from http to fo

Post by danjapro »

califdon


My orginal code worked as far as downloading the images, but it ignores my variable $dir, for location to place the file.
SEE my code here that works, you can try it for yourself.

What would be best approach to getting that done with download function reading two variables??

Code: Select all


function geturl($url, $dir) {
	$headers[] = 'Accept: image/png';
	$headers[] = 'Connection: Keep-Alive';
	$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
	$user_agent = 'ONLY_MY_HOST';
	$process = curl_init($url);
	curl_setopt($process,CURLOPT_URL,$url); 
	curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($process, CURLOPT_HEADER, 0);
	curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
	curl_setopt($process, CURLOPT_REFERER, $dir);
	curl_setopt($process, CURLOPT_TIMEOUT, 30);
	curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
	$return = curl_exec($process);
	curl_close($process);
	return $return;
	
}

function downloader($url) {
	$file_name = basename($url);
	$generated_files = geturl($url, $url);
	file_put_contents($file_name, $generated_files);
	$size = strlen($generated_files);
	if ($size == 0) {
		echo '<b>Error:</b>not found!<br />';
	}
}

echo 'images be saved to ' . getcwd() . '<br />';

$merch_map = "http://tdr.aaa.com/tdr-images/images/mapping/dynamic/aaamemberdeals/spot_map"; 
$dir = "C:/wamp/www/merchant_batch/merchant_maps";

$ext = ".png";
for($i = 1; $i < 100; ++$i) {
	$image = "{$i}{$ext}";
	$url = "$merch_map/$image";   
	echo "$url <br />";
	downloader($url, $dir);
	//echo "<img src=\"$image\" /><br />";
}
I got several feedback about using fopen, but that create a stream call to open a protect directory and return error, directory is unable to http open stream.

So using my code listed above, how would I just get it to download, but download to the set $dir (directory) not the directory where the code exists.

Can you help guide me in that aspect.

I am intermediate when it comes to coding, n GURU as you are.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: I need help with this http file transfer from http to fo

Post by califdon »

I'm afraid I don't understand what it is that this code is supposed to be doing. Is this going to be tested on your WAMP server, then deployed on the Internet? What is the purpose of this code? What will the user do and what is supposed to happen?
danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

Re: I need help with this http file transfer from http to fo

Post by danjapro »

I really need help with this,, I do not understand where to make the file_put_contents read the directory to save to.
The code is to look at the http directory, and download all those images which are listed as 1 - 800.png by name, and transfer them to $dir. I am testing it locally first then placing it online to web directory. It is one of our clients, we are reading the image from their server, but their server is
I changed my code around several times, I keep getting

Undefined variable: image
Undefined variable: dir

Division by zero.

Can you just please help me, please. I am having hardest time,, I am not that good with php and variable reading.

I place the variable per development UI, but then rendering code it errors out.

Code: Select all

$merch_map = "http://tdr.aaa.com/tdr-images/images/mapping/dynamic/aaamemberdeals/spot_map"; 
$dir = "C:/wamp/www/merchant_batch/merchant_maps";

$ext = ".png";
for($i = 1; $i < 100; ++$i) {
	$image = "{$i}{$ext}";
	$url = "$merch_map/$image";   
	echo "$url <br />";
	downloader($url, $dir);
	//echo "<img src=\"$image\" /><br />";
}

function geturl($url, $dir) {
	$headers[] = 'Accept: image/png';
	$headers[] = 'Connection: Keep-Alive';
	$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
	$user_agent = 'ONLY_MY_HOST';
	$process = curl_init($url);
	curl_setopt($process,CURLOPT_URL,$url); 
	curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($process, CURLOPT_HEADER, 0);
	curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
	curl_setopt($process, CURLOPT_REFERER, $dir);
	curl_setopt($process, CURLOPT_TIMEOUT, 30);
	curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
	$return = curl_exec($process);
	curl_close($process);
	return $return;
	
}



function downloader($url) {
	$file_name = basename($url);
	$generated_files = geturl($url, $url);
	file_put_contents($dir/$image, $generated_files);
	$size = strlen($generated_files);
	if ($size == 0) {
		echo '<b>Error:</b>not found!<br />';
	}
}


echo 'images be saved to ' . $filename . '<br />';


User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: I need help with this http file transfer from http to fo

Post by califdon »

Are you saying that the goal is for the user to go to a page on the Internet, and from there download a file from the server into a specified directory on the user's computer? If that is what you are trying to do, I don't know of any way that can be done. As far as I know, you cannot control where a downloaded file will be saved on the user's computer. I just did a little research on the use of headers, but I can find nothing that would support that. When you download a file from a server, a Save As box pops up so the user can determine where to save it.
Post Reply