Saturday, February 5, 2011

cant get php to save filename with correct name

For some reason, when using this code the file it outputs is just called ".jpg".

<?
$title = $GET['title'];
$url = $GET['url'];

$ourFileName = $title.jpg;
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);

$ch = curl_init ("http://www.address.com/url=$url");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);

$fp = fopen("images/$ourFileName",'w');
fwrite($fp, $rawdata);
fclose($fp); 

echo ("<img src=images/$ourFileName>");
?>

Pretty sure it has to do with declaring $ourFileName, any help would be appreciated.

  • Looks like it should be the following:

    $ourFileName = $title.'.jpg';
    
  • Looks like this line

    $ourFileName = $title.jpg;
    

    Should be

    $ourFileName = $title . ".jpg";
    

    That should do it as long as you are sure there is a value in $_GET['title'];

    From neilc
  • Surely should be

    $title = $_GET['title'];
    $url = $_GET['url'];
    $ourFileName = $title.'.jpg';
    

    Looks like a world of security problems there though! A script that overwrites any file it has permissions to with a file of an attackers choosing!

    From Paul Dixon
  • Are you sure $GET['title'] is correct? The superglobal variable for accessing the GET parameter values is $_GET, not $GET.

  • Try this $ourFileName = $title . ".jpg";

0 comments:

Post a Comment