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
27
Other forums
Multiple while loops
I have several DB queries that I know should be returning results and aren't. I have a feeling it ha
Problems with returning true or false in eval()'d code
Hi guys,
Would appreciate some help with a problem when running eval() on a function that sho
Show specific day of the week
I have been racking my brain trying to get this function to work properly and it is doing so for eve
Performance impact of cookies
Hi, I was just wondering what impact there would be in terms of performance if you where to set then
simple script won't work. Please help
I'm new to php, and I am having trouble displaying a simple script in my web browser. My php code ex
distinct rows
Hi
version 10.2.0.3
I have a query output something like the following
ID
DateTimeZone::listIdentifiers headache
http://php.net/manual/en/datetimezone.listidentifiers.php
So the documentation states tha
Time-based image rotation script
I'm trying to write a PHP script that rotates an image based on what time of day it is. I want the
User feedback after MySQL query has been executed
Hi all, I've just registered on PHPFreaks because I've got a question that I simply can't work out b
gmmktime or mktime
I live in the UK, i am within the GMT (with daylight saving time ie 1 hour difrence between summer a