Jump to content

How to check if there's a file to be pass on the server?


bugzy

Recommended Posts

A beginner question..

 

I have tried "isset" but it aint working.. I wonder what PHP function to use to check if a user actually put a file to be uploaded on the server. A validation  just in case a user just click the submit button w/out even choosing a file first.

 

 

I have this code but it aint working...

 

<?php
if(isset($_POST['submit']))
{

if(isset($_FILES['uploaded_file']))
	{
		echo "<span class=\"error_validation\">You haven't choose a logo to upload!<br></span>";
		echo "<span class=\"error_validation\">Upload Logo Again? <a href=\"edit_logo.php\">Click Here</a><br></span>";
	}
else
{

		if($_FILES['uploaded_file']["type"] == "image/gif")
		{


			$target_path = "logo/";

			$target_path = $target_path. basename('matzhee_logo.gif');



			if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path))
				{
					echo "<span class=\"error_validation\">The Logo has been successfully changed! Refresh the page to see the changed!<br></span>";
					echo "<span class=\"error_validation\">Upload Logo Again? <a href=\"edit_logo.php\">Click Here</a><br></span>";
				}
				else
				{
					echo "<span class=\"error_validation\">There was an error uploading the logo. Pls. try again.<br></span>";
					echo "<span class=\"error_validation\">Upload Logo Again? <a href=\"edit_logo.php\">Click Here</a><br></span>";
				}

		}
		else
		{
		echo "<span class=\"error_validation\">Invalid file format. We are only accepting image file. Pls. try again.<br></span>";
		echo "<span class=\"error_validation\">Upload Logo Again? <a href=\"edit_logo.php\">Click Here</a><br></span>";
		}
}

}
else
{
me_redirect_to("edit_logo.php");
}


?>

 

 

Anyone? thanks

Link to comment
Share on other sites

You probably wanted to use

 

if(isset($_FILES['uploaded_file']) == FALSE)

 

As of now, you're producing an error if it is set.

 

You might also want to check if it's empty

 

 

Hey thanks

 

 

I have tried both like this one

 

<?php
if(empty($_FILES['uploaded_file']))
?>

 

but it's still bypassing the validation...

Link to comment
Share on other sites

Show us your form, and try using print_r($_FILES); in your action page. It will spit out everything contained in your $_FILES array.

 

 

form

 

<?php
<form enctype="multipart/form-data" method="post"  action="<?php echo "uploaded_logo.php?edited=1" ?>">

<input type="hidden" name="MAX_FILE_SIZE" value="100000" />

<tr>
<td>Choose a Logo to Upload: </td>
    <td><input name="uploaded_file" type="file" /></td>
</tr>

<tr>
<td> </td>
    <td><input type="submit" value="Upload File" name="submit"></td>
</tr>




</form>

?>

 

 

print_r

 

Array ( [name] => [type] => [tmp_name] => [error] => 4 => 0 )

 

 

what do you think is the problem?

Link to comment
Share on other sites

http://www.php.net/manual/en/features.file-upload.errors.php

 

UPLOAD_ERR_NO_FILE

Value: 4; No file was uploaded.

 

You could try something like this for validation.

<?php

// Check if the array hasn't even been set
if( isset($_FILES['uploaded_file']) == FALSE ) {
echo 'You must choose a file to upload';

// Otherwise, check if the error key isn't empty
} elseif( empty($_FILES['uploaded_file']['error']) == FALSE ) {
// If it's not empty, report which error was found
switch( $_FILES['uploaded_file']['error'] ) {
	case UPLOAD_ERR_INI_SIZE:
	case UPLOAD_ERR_FORM_SIZE:
		echo 'File was too big';
		break;
	case UPLOAD_ERR_PARTIAL:
		echo 'Only part of the file could be uploaded';
		break;
	case UPLOAD_ERR_NO_FILE:
		echo 'No file was selected';
		break;
	case UPLOAD_ERR_NO_TMP_DIR:
		echo 'No temporary directory specified for uploads';
		break;
	case UPLOAD_ERR_CANT_WRITE:
		echo 'Could not write file to disk';
		break;
}

// Otherwise, the array is set, and theres no errors
} else {
echo 'Everything went okay with '.$_FILES['uploaded_file']['name'];
}

?>

 

There's a lot there, but it pretty much covers any error you'd run in to. Let me know if you don't understand any of it, and I'll explain with more detail.

Link to comment
Share on other sites

http://www.php.net/manual/en/features.file-upload.errors.php

 

UPLOAD_ERR_NO_FILE

Value: 4; No file was uploaded.

 

You could try something like this for validation.

<?php

// Check if the array hasn't even been set
if( isset($_FILES['uploaded_file']) == FALSE ) {
echo 'You must choose a file to upload';

// Otherwise, check if the error key isn't empty
} elseif( empty($_FILES['uploaded_file']['error']) == FALSE ) {
// If it's not empty, report which error was found
switch( $_FILES['uploaded_file']['error'] ) {
	case UPLOAD_ERR_INI_SIZE:
	case UPLOAD_ERR_FORM_SIZE:
		echo 'File was too big';
		break;
	case UPLOAD_ERR_PARTIAL:
		echo 'Only part of the file could be uploaded';
		break;
	case UPLOAD_ERR_NO_FILE:
		echo 'No file was selected';
		break;
	case UPLOAD_ERR_NO_TMP_DIR:
		echo 'No temporary directory specified for uploads';
		break;
	case UPLOAD_ERR_CANT_WRITE:
		echo 'Could not write file to disk';
		break;
}

// Otherwise, the array is set, and theres no errors
} else {
echo 'Everything went okay with '.$_FILES['uploaded_file']['name'];
}

?>

 

There's a lot there, but it pretty much covers any error you'd run in to. Let me know if you don't understand any of it, and I'll explain with more detail.

 

 

thanks so much xyph! I'm using it now and it'a an all in one validation for everything. Though I want to maximize it, where will I set up there the maximum file size allowed?

 

Thanks again!

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.