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.

Did you know?Explore Trending and Topic pages for more stories like this.
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

ereg_replace in Wordpress
Heya - so I'm working on this site: http://world-of-smiles.theportlandco.com/new-patients

The

Help with looping
I have a comma separated list of colors taken from a database ($ICo) and a directory of images named

Unable to retreve the values from Mysql Query
Hi,

Here is the php code that I have, Query is running properly in phpmyadmin and is resu

How to assign JS screen.width to ASP.Net variable
I've got this ASP.net routine that gets the cookie value of the screen width, but now I need to chan

Material xxx does not exist in plant xxx
Dear All,

I am working for a steel project which is repetitive manufacturing.

MFC GUI Programming
I am working on a project right now that requires me to create a MFC Windows GUI. Basically it need

validating url
im trying to validate url's sent to me by a form
the url's im collecting are for placing banners

Image loading
Hi Developers,

I'm trying to display an image from images/image.gif getting the url from mysq

New to PHP and just trying to understand a little code.
I hope I'm not annoying anyone or breaking the rules but I was wondering about this bit of code righ

Logging and nologging bulk insert
Hi,

oracle version: 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production

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