Jnerocorp Posted November 27, 2009 Share Posted November 27, 2009 Hello, I am trying to build a remote upload script for my image hosting site. I am using $_GET for testing purposes. this would be the url you would visit: http://jnerocorp.com/imagehost/beta2/remote.php?imgurl=http://www.phpfreaks.com/media/images/forums/logo.png This is the error I am getting: Invalid url given Im not really sure if this code would even work though I havnt been able to test it here is the code: <?php $imgurl = $_GET['img_url']; // define the filename extensions you're allowing define('ALLOWED_FILENAMES', 'bmp|gif|ico|jpg|png|tiff'); // define a directory the webserver can write to define('IMAGE_DIR', '/images'); // check against a regexp for an actual http url and for a valid filename, also extract that filename using a submatch (see PHP's regexp docs to understand this) if(!preg_match('#^http://.*([^/]+\.('.ALLOWED_FILENAMES.'))$#', $imgurl, $m)) { die('Invalid url given'); } // try getting the image if(!$img = file_get_contents($_GET['img_url'])) { die('Getting that file failed'); } $handle = fopen("$imgurl", "rb"); $filesize = fread($handle, filesize($myFile)); fclose($handle); if($filesize <= 1048576) { // try writing the file with the original filename -- note that this will overwrite any existing filename in the same directory -- that's up to you to check for if(!file_put_contents(IMAGE_DIR.'/'.$m[1], $img)) { die('Writing the file failed'); } $uid = uniqid(); $image = $m[1].$img; $master = $uid.$image; rename("/images/".$m[1].$img."", "/images/".$master.""); $fullurl = "http://JneroCorp.com/imagehost/beta2/images/".$master.""; echo "<img src='$fullurl'>"; } ?> -John Quote Link to comment https://forums.phpfreaks.com/topic/183154-help-with-this-code-please/ Share on other sites More sharing options...
KevinM1 Posted November 27, 2009 Share Posted November 27, 2009 The key you use in $_GET needs to be the same as what you pass to the script via URL. In other words: in your URL, you have imgurl, but in your code, you're trying to obtain $_GET['img_url']. Note the difference. Quote Link to comment https://forums.phpfreaks.com/topic/183154-help-with-this-code-please/#findComment-966594 Share on other sites More sharing options...
Jnerocorp Posted November 27, 2009 Author Share Posted November 27, 2009 ahh thats better that was a stupid mistake lol but now im getting real php errors: Error 1: [27-Nov-2009 13:19:57] PHP Warning: fread() [<a href='function.fread'>function.fread</a>]: Length parameter must be greater than 0 in /home/jnero/public_html/jnerocorp.com/imagehost/beta2/remote.php on line 21 Error 2: [27-Nov-2009 13:19:57] PHP Warning: file_put_contents(/images/o.png) [<a href='function.file-put-contents'>function.file-put-contents</a>]: failed to open stream: No such file or directory in /home/jnero/public_html/jnerocorp.com/imagehost/beta2/remote.php on line 27 using url: http://jnerocorp.com/imagehost/beta2/remote.php?img_url=http://www.phpfreaks.com/media/images/forums/logo.png -John Quote Link to comment https://forums.phpfreaks.com/topic/183154-help-with-this-code-please/#findComment-966596 Share on other sites More sharing options...
KevinM1 Posted November 27, 2009 Share Posted November 27, 2009 You don't define $myFile. EDIT: d'oh, didn't see the preg_match for $m Quote Link to comment https://forums.phpfreaks.com/topic/183154-help-with-this-code-please/#findComment-966597 Share on other sites More sharing options...
abazoskib Posted November 27, 2009 Share Posted November 27, 2009 You should really be using urlencode/urldecode for that GET variable. Quote Link to comment https://forums.phpfreaks.com/topic/183154-help-with-this-code-please/#findComment-966667 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.