How to upload an internet file without an extension

I'm generating a QR code that I then want to upload to my server. Problem is, the image / qr code, while it looks great on the screen, doesn't have your typical URL in that there's no extension, at least as far as I can see.

Here's the image source code: https://chart.googleapis.com/chart?cht=qr&chs=250x250&chl=http://www.countryshowdown.com/app/view_contestants.php?id=53102&choe=UTF-8&chld=L

I can tell that it's a PNG when I look at the properties, but how do I convert the above mess into something that your typical upload script will understand and execute?

Right now, I'm using this ($url is the image source code you see above):

$name = basename($url);
list($txt, $ext) = explode(".", $name);
$name = $txt.time();
$name = $name.".".$ext;
 echo $ext;
//check if the files are only image / document
if($ext == "jpg" or $ext == "png" or $ext == "gif" or $ext == "doc" or $ext == "docx" or $ext == "pdf")
{
//here is the actual code to get the file from the url and save it to the uploads folder
//get the file from the url using file_get_contents and put it into the folder using file_put_contents
$upload = file_put_contents("qr/$name",file_get_contents($url));
//check success

if($upload)  echo "Success: <a href='qr/".$name."' target='_blank'>Check Uploaded</a>"; else "please check your folder permission";
}
else
{
echo "Please upload only image/document files";
}

Open in new window


The result is the error that's cued to trigger when the code doesn't see a recognizable extension. How do I transform the above url into file name with an png extension that can then be uploaded?
brucegustPHP DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Here is how I have done it.  You can test it on my server, here:
http://www.laprbass.com/RAY_qr_code.php

<?php // RAY_qr_code.php
error_reporting(E_ALL);

// GENERATE QR CODE IMAGES
// SEE https://google-developers.appspot.com/chart/infographics/docs/overview
// SEE https://google-developers.appspot.com/chart/infographics/docs/qr_codes
// SAMPLE https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=Hello%20world
// ACHTUNG http://www.mediapost.com/publications/article/160932/qr-malware-surfaces-on-apps.html

// THE DIRECTORY FOR THE QR CODES
$dir = 'qrcodes';

// DETERMINE THE SIZE
$s = "144";
if (!empty($_GET["size"]))
{
    $s = strtoupper(trim($_GET["size"]));
    switch($s)
    {
        case "S" : $s =  72; break;
        case "M" : $s = 144; break;
        case "L" : $s = 288; break;
        case "G" : $s = 547; break; // LARGEST FROM TESTS ON 2011-10-20
    }
}
$chs = $s . 'x' . $s;

// IF THERE IS DATA
$q = NULL;
if (!empty($_GET["q"]))
{
    $q = $_GET["q"];

    // MAKE A FILE NAME (UNDERSCORES REPLACE INAPPROPRIATE CHARACTERS)
    $f = $q;
    $f = preg_replace('#[^ A-Z0-9/:?&\+\.]#i', NULL, $f);
    $f = str_replace(' ', '_', $f);
    $f = preg_replace('#[^A-Z0-9]#i', '_', $f);
    $f = preg_replace('#_+#', '_', $f);

    // CALL THE QR CODER
    $url
    = 'https://chart.googleapis.com/chart?'
    . 'cht=qr'          // CREATE A QR CODE
    . '&'
    . 'choe=ISO-8859-1' // ENCODING
    . '&'
    . "chs=$chs"        // PIXEL DIMENSIONS (SQUARE)
    . '&'
    . 'chld=Q'          // QUALITY ERROR RECOVERY IMPLIES UP TO 67 ALPHA-NUMERICS IN LEVEL 4
    . '&'
    . 'chl='            // THE URLENCODED STRING
    . urlencode($q);
    ;
    $qrc = file_get_contents($url);

    // WRITE THE IMAGE
    $lnk = $dir . DIRECTORY_SEPARATOR . "QR_CODE_$f" . '.png';
    file_put_contents($lnk, $qrc);

    // CONSTRUCT THE RESPONSE
    $txt = htmlentities($q);
$out = <<<OUT
    <p>Voila! Here is the QR code for <b>$txt</b></p>
    <p><img src="$lnk" title="$f" /></p>
    <p>Right-click and save the image, or use this: <a target="_blank" href="$lnk">$f.png</a></p>
OUT;
    echo $out;
}

$form = <<<FORM
Enter up to 67 characters to see the QR code (maybe a URL?)
<form>
<input type="text"   name="q"    value="$q" size="67" style="font-family:Courier;" /><br/>
<input type="radio"  name="size" value="S" />Small (probably too small)<br/>
<input type="radio"  name="size" value="M" />Medium (probably just right)<br/>
<input type="radio"  name="size" value="L" />Large<br/>
<input type="radio"  name="size" value="G" />Gigantic<br/>
<input type="submit"             value="Get QR code" />
</form>
FORM;

echo $form;

Open in new window

You may want to read the man page references... Just a thought, ~Ray
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brucegustPHP DeveloperAuthor Commented:
Ray, not that I would expect anything less from one of my favorite ninjas, but this awesome!

Question: When bring up the image at http://www.countryshowdown.com/qr/QR_CODE_http_www_countryshowdown_com_contestant_php_id754.png, it shows up just fine, but the image itself isn't anywhere on my server. In other words, when I go looking in the qr directory, it isn't there. Should it be? Am I missing something?
0
Ray PaseurCommented:
I don't know.  It looks like the script sends the image directly to the browser output stream, but without looking at the code that created the image and sent it to the browser, I would not be able to tell if it wrote the image file into a directory, too.  Please post the script and I'll take a look.
0
brucegustPHP DeveloperAuthor Commented:
Never mind, Ray! I got it! Thanks so much!
0
Ray PaseurCommented:
Cool!  Nite nite!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.