Jump to content

Need help making a php file upload script work on a server.


Redlightpacket

Recommended Posts

I know there is a bug in my code or something. I need somebody that could help me make my code work. My code is explained in some parts.

 

 

I just need  my code to upload files to a server. If this code isn't a workable type, just give me some of yours.

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<body>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>File Upload</title>
</head>
<form enctype="multipart/form-data" action="newphpupload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288" />
<fieldset><legend>Select a JPEG or GIF image to be uploaded: </legend>

<p><b>File:</b><input type="file" name="upload" /></p>
</fieldset>

<div align="center"> <input type="submit" name="submit" vaule="Submit"/></div>
<input type="hidden" name="submitted" value="TRUE" />

</form>

</body>
</html>

 

 

 

 

 

<?php



// Check if the form has been submitted.
if (isset($_POST['submitted'])) {


//Check for an uploaded file.
if(isset($_FILES['/home/www/'])) {

// Validate the type. Should be jpeg, jpg, or jif.
$allowed = array('image/gif', 'image/jpeg', 'image/jpg');
if (in_array($_FILES['/home/www/']['type'], $allowed)) {


// Move the file over

if (move_uploaded_file($_FILES['/home/www/']['/home/www/'], "uploads/{$_FILES['/home/www/']
['name']}")) {

echo ' <p> The file has been uploaded!</p>';

} else { // Couldn't move the file over.

echo '<p> <font color="red">The file could not be uploaded because: <b>';

// Print a message based upon the error

switch($_FILES['upload']['error']){

case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini.';
break;

case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
break;

case 3:
print 'The file was only partially uploaded.';
break; 

case 4:
print 'No file was uploaded.';
break;


case 6:
print 'No temporary folder was available.';
break;

default:
print 'A system error occrured.';
break;

print '</br></font>.</p>';

}// End of  move... IF.

}

} else { // Invalid type.
echo '<p><font color="red">Please upload a JPEG or GIF image.</font></p>';
echo'<p><font color="red">Please upload a JPEG or GIF image.</font></p>';
unlink($_FILES['upload']['/home/www/']); // Delete the file

}


} else { // No file uploaded
echo 'Please upload a JPEG';
}


} // End of the submitted conditional.
?>

you have a few mistakes, try this PHP code

**UNTESTED*

<?php
// Check if the form has been submitted.
$uploaddir = '/home/www/uploads/'; //I assume this is the upload path
if (isset($_POST['submitted'])) {
//Check for an uploaded file.
if(isset($_FILES['upload']['name']))
{
	$uploadfile = $uploaddir . basename($_FILES['upload']['name']);
	// Validate the type. Should be jpeg, jpg, or jif.
	$allowed = array('image/gif', 'image/jpeg', 'image/jpg');
	if (in_array($_FILES['upload']['type'], $allowed))
	{
	// Move the file over
		if (move_uploaded_file($_FILES['upload']['tmp_name'], $uploadfile))
		{
			echo ' <p> The file has been uploaded!</p>';
		} else { // Couldn't move the file over.
			echo '<p> <font color="red">The file could not be uploaded because: <b>';
			// Print a message based upon the error
			switch($_FILES['upload']['error'])
			{
			case 1:
				print 'The file exceeds the upload_max_filesize setting in php.ini.';
			break;
			case 2:
				print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
			break;
			case 3:
				print 'The file was only partially uploaded.';
			break; 
			case 4:
				print 'No file was uploaded.';
			break;
			case 6:
				print 'No temporary folder was available.';
			break;
			default:
				print 'A system error occrured.';
			break;
			}// End of  move... IF.
			print '</br></font>.</p>';
		}
	} else { // Invalid type.
		echo '<p><font color="red">Please upload a JPEG or GIF image.</font></p>';
		echo'<p><font color="red">Please upload a JPEG or GIF image.</font></p>';
		unlink($_FILES['upload']['tmp_name']); // Delete the file
	}
} else { // No file uploaded
	echo 'Please upload a JPEG';
}
} // End of the submitted conditional.
?>

I get these error messages when I put the script on the server.

 

=====================================================================

Warning: move_uploaded_file(/www/codehouse.100webspace.net/ Baby.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/www/codehouse.100webspace.net/newphpupload.php on line 14

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpavQ7Ax' to '/www/codehouse.100webspace.net/ Baby.jpg' in /home/www/codehouse.100webspace.net/newphpupload.php on line 14

 

The file could not be uploaded because: A system error occrured.

====================================================================

 

Here is my php code:

---------------------

 

<?php
// Check if the form has been submitted.
$uploaddir = '/www/codehouse.100webspace.net/ '; //I assume this is the upload path
if (isset($_POST['submitted'])) {
//Check for an uploaded file.
if(isset($_FILES['upload']['name']))
{
	$uploadfile = $uploaddir . basename($_FILES['upload']['name']);
	// Validate the type. Should be jpeg, jpg, or jif.
	$allowed = array('image/gif', 'image/jpeg', 'image/jpg');
	if (in_array($_FILES['upload']['type'], $allowed))
	{
	// Move the file over
		if (move_uploaded_file($_FILES['upload']['tmp_name'], $uploadfile))
		{
			echo ' <p> The file has been uploaded!</p>';
		} else { // Couldn't move the file over.
			echo '<p> <font color="red">The file could not be uploaded because: <b>';
			// Print a message based upon the error
			switch($_FILES['upload']['error'])
			{
			case 1:
				print 'The file exceeds the upload_max_filesize setting in php.ini.';
			break;
			case 2:
				print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
			break;
			case 3:
				print 'The file was only partially uploaded.';
			break; 
			case 4:
				print 'No file was uploaded.';
			break;
			case 6:
				print 'No temporary folder was available.';
			break;
			default:
				print 'A system error occrured.';
			break;
			}// End of  move... IF.
			print '</br></font>.</p>';
		}
	} else { // Invalid type.
		echo '<p><font color="red">Please upload a JPEG or GIF image.</font></p>';
		echo'<p><font color="red">Please upload a JPEG or GIF image.</font></p>';
		unlink($_FILES['upload']['tmp_name']); // Delete the file
	}
} else { // No file uploaded
	echo 'Please upload a JPEG';
}
} // End of the submitted conditional.
?>

 

Any help would be nice.

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.