Jump to content

Uploading Images... help with my code?


bri0987

Recommended Posts

I working on uploading images to the host machine.

 

How does my code look.

 

Also, I need help with being able to rename the file that gets uploaded and help with "Validating if FILE already Exists"

 

Please post replies respectfully.

 

	  <?php
// Loop through file upload fields PICTURES from HTML FORM
foreach ($_FILES["pictures"]["error"] as $key => $error) { 
  		$size = $_FILES["pictures"]["size"][$key];
		$type = $_FILES["pictures"]["type"][$key];
		$name = $_FILES["pictures"]["name"][$key];
	// Validate if ERROR occured
	if ($error == UPLOAD_ERR_OK) {
		echo $name . "<br />";
		// Validate the FILE SIZE
		if (($size < 100000) && ($size > 0)) {
			echo "Size of $name is good: " . $size . "<br />";
			$size_okay = 1;
			} else {
			echo "The file size of " . $name . " is to large. 100KB is the Maximum"; 
			$size_okay = 0; 
			}
		// Validate the FILE TYPE EXTENSION
		$filename = explode(".", $_FILES["pictures"]["name"][$key]);
		$filenameext = $filename[count($filename)-1];
		if (($filenameext == "jpg") || ($filenameext == "JPG") || ($filenameext == "jpeg")) {
			echo $filenameext . "<br />";
			echo $name . " is a JPG file type. <br />";
			$type_okay = 1;
			} else {
			echo $name . " is NOT a JPEG or JPG file type... it is a \"" . $filenameext . "\".<br />";
			$type_okay = 0;
		}
		// Validate the FILE NAME
		if ($name == $name) {
			echo "The " . $name . " is good. <br />";
			$name_okay = 1; 
			} else {
			echo "The " . $name . " is BAD. <br />";
			$name_okay = 0;
		} 
		// Validate if FILE already Exists

			   // ...... IF STATEMENT HERE... etc etc etc .......
			   
		// Uploading FILES
		if (($size_okay == 1) && ($type_okay == 1) && ($name_okay == 1)) {
			$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
			$dirpath = "components/";
			$img_name = $_FILES["pictures"]["name"][$key];
			move_uploaded_file($tmp_name, $dirpath . $img_name);
			echo "<strong>" . $name . "</strong> has been sucessfully uploaded. <br />";
			} else {
				if ($size_okay == 0) {
					echo "<strong>" . $name . 
					"</strong> has not been uploaded because the file size was greater then 100KBs."; 

				} elseif ($type_okay == 0) {
					echo "<strong>" . $name . 
					"</strong> was not uploaded because the file type was not JPG.";

				} elseif ($name_okay == 0) {
					echo "<strong>" . $name . 
					"</strong> was not uploaded because the file already exists.";

				} else {
					echo "";
			}
		}
	} else {
		echo "An error has occured during the upload of <strong>" . $name . "</strong>
		<br />1.) Make sure that the image file type is JPG or JPEG.
		<br />2.) Make sure the the image file size is NOT larger then 100 KBs"; 
	}
	echo "<br /><br />";
}

?> 

Link to comment
https://forums.phpfreaks.com/topic/74024-uploading-images-help-with-my-code/
Share on other sites

to rename the file

 


$filename =  "new file name goes here";

//if magic quotes is off
$newfilename = stripslashes($filename);

//Move the File to the Directory of your choice
//move_uploaded_file('filename','destination') Moves afile to a new location.
move_uploaded_file($_FILES['imagefile']['tmp_name'],$upload_dir.$newfilename);

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.