Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/183154-help-with-this-code-please/
Share on other sites

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.