Jump to content

Upload from Link Instead?


Warptweet

Recommended Posts

Here is the BASE for my uploader code, which will be later heavily edited. It works perfectly, absolutely no problem. Although, how would I upload a file through it's direct source link on the internet OR from files on your computer?

 

Here is my code. For this code, you can only upload from the location on your COMPUTER, but I need it to do that, AND be able to upload from a link on the internet. Thanks for any help!

 

<?php

function generate_rand($l){
$c= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
for($i=0; $i<$l; $i++)
{
$rand.= $c[rand()%strlen($c)];
}
return $rand;
}

$uploaddir = "./";
$maxfilesize = 1048576; //1 megabyte
$filename = $_FILES['file']['name'];
$filesize = $_FILES['file']['size'];
$filetmpname = $_FILES['file']['tmp_name'];
$valid = array (".jpg",".gif",".png");
if ($filename) {
$error = "";
if ($filesize == 0) {
$error .= "The submitted file was invalid.<br />";
}
$type = strtolower(strstr($filename, '.'));
if (!in_array($type, $valid)) {
$error .= "The submitted file was of invalid type.<br />";
}
if ($filesize>$maxfilesize) {
$error .= "The submitted file was larger than a Megabyte.<br />";
}
$randnum = generate_rand(10);
$randnum .= $type;
$file_exists = true;
while ($file_exists) {
if (file_exists("$uploaddir$randnum")) {
$randnum = generate_rand(10);
$randnum .= $type;
}else{
$file_exists = false;
}
}
if ($error == "") {
if (move_uploaded_file($filetmpname, "$uploaddir$randnum")) {
chmod("$uploaddir$randnum", 0644);
echo "Your file was successfully uploaded!<br />";
echo "<a href='".$uploaddir$randnum."'>Click here to go to your upload!</a>";
}
else
{
echo "Your file could not be uploaded.";
} 
}else{
echo $error;
}
}else{
echo "No file was uploaded";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/44567-upload-from-link-instead/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.