Forum Moderators: coopster

Message Too Old, No Replies

Basic file get contents errors

errors using file_get_contents

         

madgadgets

7:13 pm on Jul 12, 2007 (gmt 0)

10+ Year Member



I'm a newbie and have been learning PHP on and off for a while now.
I am trying to rebuild my website after my host dissapeared along with all my sites data, and I am very nearly 100% complete to the point prior to data loss.

However, one of the pieces of code written by another programmer is simply baffling me at the moment, and I think it may be due to the fact the code was badly written for a much older version of PHP compared to the version the site is on now with my new host etc

The code should be searching for images from one of my suppliers websites but it runs and does nothing at all...I have checked the suppliers URL's and they match the code so what is going on here....am I missing something really simple?

A sample URL is as follows:-
so the URL constant is always http://www.example.com/prod_images/

and the code from my site is below :-

<?php
$play=time();
include "connection.php";
$id=(isset($_REQUEST[id]))?$_REQUEST[id]:0;
$resource=mysql_query("SELECT * FROM `gadgets_product` WHERE `OriginalUrl`!= '' AND `id`>'$id';");
for($i=0;$i<mysql_num_rows($resource);$i++){
$array=mysql_fetch_array($resource);
$id=$array['id'];
$field=$array['OriginalUrl'];
if(time()-$play>20)break;
$htm=str_replace("!","-",str_replace("'","-",str_replace("®","-",str_replace("/","-",str_replace("\"","-",str_replace(" ","-",$field))))));
$img=file_get_contents("http://www.example.com/prod_images/".$htm.".htm.");
$startsrc=strpos($img,"<td width=\"75\"><div align=\"left\"><img src=\"",0);
$startsrc+=43;
$endsrc=strpos($img,"\"",$startsrc);
$src=substr($img,$startsrc,$endsrc-$startsrc);
$srco=explode("thumbs/150/thumb_",$src);
$srco=$srco[0].$srco;
mysql_query("UPDATE `gadgets_product` SET `ico_file` = '$src' , `image_file` = '$srco' , `OriginalUrl` = '' WHERE `id` = '$id'");
header("Location:imageupload.php?id=$id");
}header("Location:imagecopy.php");
?>

I then use the following code to upload the files onto my own site :-
<?php
$play=time();
include "connection.php";
$id=(isset($_REQUEST[id]))?$_REQUEST[id]:0;
$resource=mysql_query("SELECT * FROM `gadgets_product` WHERE `image_file` LIKE 'http:%' AND `id` >= '$id';");
$rows=mysql_num_rows($resource);
for($k=0;$k<$rows;$k++){
$array=mysql_fetch_array($resource);
$id=$array['id'];
$htm=$array['image_file'];
if(time()-$play>20)header("Location:imagecopy.php?id=$id");
$ext=".jpg";
$recurso = @imagecreatefromjpeg($htm);
if(!$recurso){
$ext=".gif";
$recurso = @imagecreatefromgif($htm);
}
if($recurso){
$arreglo=getimagesize($htm);
for($i=0;$i<2;$i++){
if($i==0){
$pathdestino="products/".$id.$ext;
$ancho=180;
$alto=180;
}else{
$pathdestino="products/".$id.$ext;
$ancho=280;
$alto=280;
}
$relacionalto=$alto/$arreglo[1];
$relacionancho=$ancho/$arreglo[0];
if($relacionalto<$relacionancho)$ancho=$arreglo[0]*$relacionalto;
else $alto=$arreglo[1]*$relacionancho;
if($ancho>$arreglo[0]¦¦$alto>$arreglo[1]){
$ancho=$arreglo[0];
$alto=$arreglo[1];
}
$nueva=imagecreatetruecolor($ancho,$alto);
imagecopyresampled($nueva,$recurso,0,0,0,0,$ancho,$alto,$arreglo[0],$arreglo[1]);
imagejpeg($nueva,$pathdestino,100);
}
}
mysql_query("UPDATE `gadgets_product` SET `ico_file` = '".$id.$ext."' , `image_file` = '".$id."_full".$ext."' WHERE `id` = '$id'");
header("Location:imagecopy.php?id=$id");
}
?>

I would appreciate any advice from anyone with greater experience as I have spent around a week trying to get this code to function without any success!
Nick

[1][edited by: eelixduppy at 7:20 pm (utc) on July 12, 2007]
[edit reason] example.com [/edit]

eelixduppy

7:35 pm on Jul 12, 2007 (gmt 0)



Welcome to WebmasterWorld!

You say you are getting errors? Could you please share them and their respective lines with us, please?

RonPK

11:51 am on Jul 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$img=file_get_contents("http://www.example.com/prod_images/".$htm.".htm.");

My guess is that the dot causes problems. Try this:

$img = file_get_contents('http://www.example.com/prod_images/' . $htm . '.htm');

madgadgets

12:53 pm on Jul 13, 2007 (gmt 0)

10+ Year Member



I have made the amendments you suggested but it still simply runs (without errors) but does nothing at all...I can't seem to understand why it is not picking up the images and loading them into my SQL database etc

RonPK

1:20 pm on Jul 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, in that case you'll need to debug the script. You might start by making the script die whenever the update-query fails:

$sql = "UPDATE ... (et cetera)"; 
mysql_query($sql) or die(mysql_error() . "\n\nquery: $sql");