Jump to content

[SOLVED] Return True upon file upload


GregL83

Recommended Posts

So another noob question from GregL83 :) .

I want to run some code only if a file is selected to be uploaded.

if(is_uploaded_file($_FILES["file"])) {
	if(($_FILES["file"]["size"] < 250000)) {
		if($_FILES["file"]["error"] > 0) {
		echo "Error: " . $_FILES["file"]["error"] . "<br />";
		}
		else {
		echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
		echo "Stored in: " . $_FILES["file"]["tmp_name"]."<br/>";
		echo "Type: " . $_FILES["file"]["type"] . "<br />";
		$ftype = substr($_FILES["file"]["type"],(strlen($_FILES["file"]["type"]))-3);
		echo $ftype."<br/>";
		move_uploaded_file($_FILES["file"]["tmp_name"], "images/main_image.".$ftype);
		echo "Stored in: "."images/".$_FILES["file"]["name"];
		}
	}
}

The line of importance is at the top!  I want the functions to run if a file is uploaded.  Let me know if you need the form, but i dont think it is nessecary to identify the proper method.  Thanks once again.  You guys are great!

Link to comment
Share on other sites

Something else I tried is:

if($_POST['file']) {}

Problem with this is I get a undefined index at the line because the variable $_POST['file'] doesn't exist if no file is uploaded...  How do i get php to run a bit of code only if the the variable exists?

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.