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

Printer configuration
Hi All,
Suppose one of the user deleted the printer settings unfortunatly ,then what is the pro

Weird problem with SELECT command..Help!
Hi!

It seems I'm having a really weird problem with SQL SELECT command....I have table into a

C - Reading a file into a byte array
Hi,

I'm trying to read a file into a byte array in C. I have to use C as this is for a loadru

Check Date/Time in PHP
What I need the code to check is that $reQuest is 24 hours in the past then return a time based on h

Is there a way to override built in php functions without APD?
I am trying to use the
rename_function()
override_function()
options that are built into

array help
Does anyone know how to require 10 text fields for individual grades and output class average? (10 p

Spliting paragraph into sentences and attach in
Here is what I am trying to do

example: Para1[123.456.789!] 3 sentences
Para2

wav to mp3 + goDaddy
I need to convert wav files to mp3s. From what I have found so far one way to do it is to have my PH

Advice on how to delete a mysql row using my form
Hi,

I am very new to php and am struggling to work out how to delete a portfolio item (a row

how do i display data on a page from mysql
1 - do i use this code at the top of each of my page i wish to only alow access if there as been a s

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