Jump to content

Upload Via url


NewGuy21

Recommended Posts

Ok I finally finished my image host. But I wanna add option to upload via url.

 

I have this as the form

<FORM ENCTYPE="multipart/form-data" ACTION="upload.php" METHOD=POST>
<table width="100%" border="0" cellpadding="2" cellspacing="0" align="center">
<tr>
	<td align="right" valign="top"><span class="body"><strong>Your File:</strong></span></td>
	<td align="left"><input type="File" name="thefile" size="30" maxlength="255"></td>
</tr>

 

It was trying to add a radial button. So you can add via url.

 

But when I add the radial it doesnt work.

 

Here is the copy of the php that  uploads the file.

 

<?

// get variables for fields on upload screen
$thefile = $_FILES['thefile'];
$tos = $_POST['tos'];

$currentip = $_SERVER['REMOTE_ADDR'];
$messages = "";

// check for blocked ip address
if ($currentip != "") {
	$link = mysql_connect($db_server, $db_user, $db_password) or die("Could not connect to the database.");
	mysql_select_db($db_name) or die("Could not select the database.");
	$query = "select ip from blocked where ip = '$currentip'";
    $result = mysql_query($query) or die("Query failed.");
	$num_rows = mysql_num_rows($result);
	if ($num_rows > 0) {
		$messages .= "Your IP address (" . $currentip . ") has been blocked from using this service.<br><br>";
	}
}

// check for a file
if (!isset($thefile) || $thefile['name'] == "") {
	$messages .= "You must select a file to upload using the "Browse" button.<br><br>";
}

if ($messages == "") {
	// check for valid file type
	if (!in_array_nocase($thefile['type'], $valid_mime_types)) {
		$messages .= "<em>" . $thefile['name'] . "</em> is not in a valid format (" . $valid_mime_types_display . ").<br><br>";
	}

	// check for valid file extension
	$thefile['name'] = strtolower($thefile['name']);
	$file_ext = substr($thefile['name'], strlen($thefile['name']) - 3, strlen($thefile['name']));
	// fix for JPEG names files
	if ($file_ext == "peg") {
		$file_ext = "jpg";
	}
	if (!in_array_nocase($file_ext, $valid_file_ext)) {
		$messages .= "<em>" . $thefile['name'] . "</em> (".$file_ext.") does not have a valid file extension (" . $valid_mime_types_display . ").<br><br>";
	}

	// check for valid file size
	if ($thefile['size'] > ($max_file_size_kb * 1024)) {
		$messages .= "<em>" . $thefile['name'] . "</em> is bigger than the allowed file size of " . $max_file_size_kb . " K.<br><br>";
	}
}

// save the file, if no error messages
if ($messages == "") {
	$max_file_size_byes = $max_file_size_kb * 5024;

	// SAVE THE PICTURE
	$newFileName = newImageName($thefile['name']);
	$newFile = $server_dir . $newFileName;
	$newFileUrl = $image_url . $newFileName;
	$newFileUrlLink = $server_save_directory . $newFileName;
	if ($file_ext == "jpg" || $file_ext == "png" || $file_ext == "gif") {
		$tnFileName = "tn_" . substr($newFileName, 0, strlen($newFileName) - 3) . "jpg";
		$tnFile = $server_dir . $tnFileName;
		$tnFileUrl = $image_url . $tnFileName;
	} else {
		$tnFileName = "";
		$tnFile = "";
		$tnFileUrl = "";
	}
	$filesize = $thefile['size'];
	$newID = "";
	if (!@copy($thefile['tmp_name'], $newFile)) {
		$messages .= "An unexpected error prevented your file from being saved. Please try again.";
	} else {
		// add to database
		$query = "INSERT INTO images (filename, tn_filename, filepath, ip, filesize) VALUES ('$newFileName', '$tnFileName', '$server_save_directory', '$currentip', $filesize)";
		mysql_query($query) or die("Database entry failed.");
		$newID = mysql_insert_id();
	}

	if ($file_ext == "jpg" || $file_ext == "png" || $file_ext == "gif") {
		if ($file_ext == "jpg") {
			$source_id = imagecreatefromjpeg($newFile);
		} elseif ($file_ext == "png") {
			$source_id = imagecreatefrompng($newFile);
		} elseif ($file_ext == "gif") {
			$source_id = imagecreatefromgif($newFile);
		}

		$true_width = imagesx($source_id);
		$true_height = imagesy($source_id);

		// create thumb
		if ($true_width > $thumbnail_size_max || $true_height > $thumbnail_size_max) {
			if ($true_width >= $true_height) {
				$dest_width = $thumbnail_size_max;
				$dest_height = ($dest_width / $true_width) * $true_height;
			} else {
				$dest_height = $thumbnail_size_max;
				$dest_width = ($dest_height / $true_height) * $true_width;
			}
			if ($thefile['type'] == "image/jpeg" || $thefile['type'] == "image/pjpeg") {
				$target_id = imagecreatetruecolor($dest_width, $dest_height);
			} elseif ($thefile['type'] == "image/png" || $thefile['type'] == "image/x-png") {
				$target_id = imagecreatetruecolor($dest_width, $dest_height);
			} else {
				$target_id = imagecreatetruecolor($dest_width, $dest_height);
			}
			$target_pic = imagecopyresized($target_id, $source_id, 0, 0, 0, 0, $dest_width, $dest_height, $true_width, $true_height);

			// create a thumbnail in JPEG format
			imagejpeg($target_id, $tnFile, $thumbnail_quality);

			imagedestroy($target_id);
		} else {
			copy($newFile, $tnFile);
		}
	}
}
mysql_close($link);

// create URL links to display to user
$showURL1 = false;  // image on hosted page - image only
$showURL2 = false;  // direct link to file - all
$showURL3 = false;  // HTML for img - image only
$showURL4 = false;  // [img][/img] tags - image only
$showURL5 = false;  // thumbnail pic - image only

// determine flags
$showURL2 = true;
if ($file_ext == "jpg" || $file_ext == "gif" || $file_ext == "png" || $file_ext == "bmp") {
	$showURL1 = true;
	$showURL3 = true;
	$showURL4 = true;
}
if ($file_ext == "jpg" || $file_ext == "gif" || $file_ext == "png") {
	$showURL5 = true;
}

function newImageName($fname) {
	$timestamp = time();
	$new_image_file_ext = substr($fname, strlen($fname) - 3, strlen($fname));
	if ($new_image_file_ext == "peg") {
		$ext = ".jpg";
	} else {
		$ext = "." . $new_image_file_ext;
	}
	$newfilename = randString() . substr($timestamp, strlen(timestamp) - 4, strlen(timestamp)) . $ext;
	return $newfilename;
}

function randString() {
	$newstring="";
	while(strlen($newstring) < 3) {
		$randnum = mt_rand(0,61);
		if ($randnum < 10) {
			$newstring .= chr($randnum + 48);
		} elseif ($randnum < 36) {
			$newstring .= chr($randnum + 55);
		} else {
			$newstring .= chr($randnum + 61);
		}
	}
	return $newstring;
}

function in_array_nocase($item, $array) {
	$item = &strtoupper($item);
	foreach($array as $element) {
		if ($item == strtoupper($element)) {
			return true; 
		}
	}
	return false;
} 

?>

 

I found this code to upload via url.

 

<?php
if ($_GET[xfer]) {
if ($_POST[from] == "") {
print "You forgot to enter a url.";
} else {
copy("$_POST[from]", "$_POST[to]");
$size = round((filesize($_POST[to])/1000000), 3);
print "transfer complete.<br>
<a><a href=\"$_POST[from]\">$_POST[from]</a><br>
<a><a href=\"$_POST[to]\">$_POST[to]</a> : $size MB";
}
} else {
print "<form action=\"$PHP_SELF?xfer=true\" method=post>
from(http://): <input name=from><br>
to(filename): <input name=to><br>
<input type=submit value=\"transload\">";
}
?>

 

So im trying to incorporate into what ive already done. I would greatly appreciate it if someone could help me.

 

Thank you ahead of time

Link to comment
Share on other sites

Adding a radial button similar to the one on imageshack.us.

 

They have 3 radial buttons above the upload imput.

 

I was just gonna add one additional radial. To upload an image via url. Instead of acctually downloading the file then uploading it, you just put in the addy of the image and it uploads it.

 

Thats what im trying to do.

 

And when I say doesnt work, I mean i cant incorporate it into the code I already have. I dont know how to do it really. I mean I tried editing my code and it just completely broke it. I ran it through my php debugger and had so many errors was crazy.

 

So im asking for help on how to do this.

Link to comment
Share on other sites

Ahh, those are 'radio' buttons, not radial :)

 

First you need to add the radio buttons to your form and assign them a name, then you would need to modify the PHP file that the form submits to so that it checks for the value of the radio buttons and either runs your normal code (assuming it works) to process the uploaded file or runs new code to save a URL image instead of an uploaded one.. Saving the URL image can be done using fsockopen() and fread() functions to read in the image, and then use the fopen() and fwrite() functions to save the data to disk.. You could also save the image into a database instead of writing it to disk..

 

Does that help?

 

Link to comment
Share on other sites

A lil but how do I exactly add the code for the url into my other code. Thats the problem im having. I have no problem making the lil button with the checks. Just I dont know exactly how to incorporate it into my other code.

Link to comment
Share on other sites

If you are not sure how to check the value of the radio buttons in the PHP code, it would be something like this:

 

if ($_POST['radio_name'] == 'url') {
  // put code here to process a url submission
} else {
  // put your original code here to process regular file submissions
}

 

Note that your form is also going to need a regular text input field for people to type in the URL.. Your example of imageshack.us has this extra field too, but they are using javascript to try and make it seem like only one field by hiding the file input box if you select 'URL' and vice-versa..

 

If you aren't sure what to do to process the url submission, then you should probably read up on fsockopen(), fread(), and fwrite(), since those are pretty much the only functions you need to use in order to accomplish this..

 

Link to comment
Share on other sites

Ty. I think I have started to figure it out I found this base code. From guy named Nico.

function transload()
        {
                if (ini_get('allow_url_fopen') == 0)
                {
                        $this->set_error($this->default_errors[15]);
                        return false;
                }
       
                if (!$this->is_valid_upload_path())
                {
                        return false;
                }
        
                if (!empty($this->transload))
                {
                        $filename = basename($this->transload);
                        $contents = '';
                       
                        if (!$this->is_valid_file($filename, false))
                        {
                                return false;
                        }

                        set_time_limit(0);
                        @ini_set('user_agent', 'PHP');

                        if (!($fp = @fopen($this->transload, 'rb')))
                        {
                                $this->set_error($this->default_errors[14]);
                                return false;
                        }
                       
                        while (!feof($fp))
                        {
                                $contents .= fread($fp, 8192);
                               
                                if ($this->max_size AND (strlen($contents) > $this->max_size))
                                {
                                        $this->set_error(sprintf($this->default_errors[9], $this->max_size));
                                        return false;
                                }
                        }
                       
                        fclose($fp);
       
                        if ($upload = @fopen($this->upload_path . $this->subdir . $this->rand_name($filename), 'w'))
                        {
                                fwrite($upload, $contents);
                                fclose($upload);
                        }
                       
                        $this->uploaded_files[] = $filename;
                        return true;
                }
        }

 

Now the problem Im having is getting the url through that code... to my other code

 

// get variables for fields on upload screen
$thefile = $_FILES['thefile'];
$tos = $_POST['tos'];

$currentip = $_SERVER['REMOTE_ADDR'];
$messages = "";

 

But ty so much you have helped me alot

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.