help with this code please?


Posted on 16th Feb 2014 07:03 pm by admin

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:
Code: http://jnerocorp.com/imagehost/beta2/remote.php?imgurl=http://www.phpfreaks.com/media/images/forums/logo.png
This is the error I am getting:
Code: 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'>";
}
?>


No comments posted yet

Your Answer:

Login to answer
141 Like 27 Dislike
Previous forums Next forums
Other forums

What am I missing here? Help!
Hello all!. I can't seem to get this working right. Well - it renders right, but something is goin

SCO Unix
I know this might not be the place to ask, but, can anyone tell me if SCO Unix comes with PHP built

Text to picture Generator
Hello, i have found this script and it works really good^^ But i have one problem, i would like to c

Removing Title From database problem
Hi again ! i am having an issue with updating database. When i update any price of a title it remove

media recovery
Hi,
i startup the database i got the error

SQL> startup
ORACLE instance started.

smart reading from a text file
Hello there fellow coders, i was wondering if one of you wouldnt mind helping me with this problem i

Format timestamp from mysql
When I tried this:
Code: date("m/d/Y H:i A", $row['timestamp'])
I got 12/31/1969 18:

Pre-Fill out a PHP form...??
This is for work actually (geek squad). We have to fill out this online php form at work over and ov

Error In Syntax
I got this error:

Code: Parse error: syntax error, unexpected '>' in /home/bucket/publ

How to add an image/C++ OpenGL?
Hi,

I have been trying to look online for this and, so far, I haven't found anything usef

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash