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
Email to a friend script problems
I have this send-to-a-friend script with 2 issues:
1) When you open the form popup on a p
Upload file!
Ok i have a form..
Code: <form name="form1" method="post" action=&quo
convert static html data into dynamic
hi all
i need to convert a website which has static above 2000 articles into dynamic and add
Converting from one format to another
"I have got a date in DD/MM/YYYY but I need it in MM-DD-YYYY. Help!"
Libraries in C++
Hi all,
I have two libraries. one is based targeted on linux platform and uses another li
Calculating a rating by adding number of points and dividing by number of items
I have a site that users can post links to files to download. They can rate these files on a 1-5 sca
Problem with PHP code- simple contact form
I'm relativily new to PHP; I know HTML and CSS stuff but I have a problem- I have a contact form wit
User registration and login
I don't know whether this is the correct forum, if not i apologise but am pretty desperate at the mo
Turning Data into URLs?
This may seem very basic to you, but I'm having a hard time figuring out how to do this since I don'
Most basic form question ever?
Hello,
I want to use this snippet to make sure the fields in a form are ok before processing