Jump to content

PHP Image Uploader not working.


gergy008

Recommended Posts

The folder 'avatar' has full permissions, 777, But this script it saying there isn't enough permissions...

 

Here is my code that processes the code sent through the form, And the form itself.

 

if(isset($_POST['submit'])){
	function error($error, $location, $seconds = 5) 
	{ 
		header("Refresh: $seconds; URL=\"$location\""); 
		echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n". 
		'"http://www.w3.org/TR/html4/strict.dtd">'."\n\n". 
		'<html lang="en">'."\n". 
		'    <head>'."\n". 
		'        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n". 
		'        <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n". 
		'    <title>Upload error</title>'."\n\n". 
		'    </head>'."\n\n". 
		'    <body>'."\n\n". 
		'    <div id="Upload">'."\n\n". 
		'        <h1>Upload failure</h1>'."\n\n". 
		'        <p>An error has occured: '."\n\n". 
		'        <span class="red">' . $error . '...</span>'."\n\n". 
		'         The upload form is reloading</p>'."\n\n". 
		'     </div>'."\n\n". 
		'</html>'; 
		exit;
	} 
	$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']); 

	$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'avatar/'; 

	$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'do_avatar.php'; 

	$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'users.php?id='.$_SESSION['login']; 

	$fieldname = 'file'; 

	$errors = array(1 => 'php.ini max file size exceeded', 
					2 => 'html form max file size exceeded', 
					3 => 'file upload was only partial', 
					4 => 'no file was attached'); 

	isset($_POST['submit']) 
		or error('the upload form is neaded', $uploadForm); 

	($_FILES[$fieldname]['error'] == 0) 
		or error($errors[$_FILES[$fieldname]['error']], $uploadForm); 

	@is_uploaded_file($_FILES[$fieldname]['tmp_name']) 
		or error('not an HTTP upload', $uploadForm); 

	@getimagesize($_FILES[$fieldname]['tmp_name']) 
		or error('only image uploads are allowed', $uploadForm);

	$uploadFilename=$_SESSION['login'];

	@imagepng($_FILES[$fieldname]['tmp_name'], $uploadFilename)
		or error('receiving directory insuffiecient permission', $uploadForm); 
		  
	header('Location: ' . $uploadSuccess); 

	exit;
} else { 
	$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']); 

	$max_file_size = 1000000;
}

 

Form:

 

// Ignore the following, It's for the PHP tag on this forum: ?>
<form id="Upload" action="<?php $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data" method="post"> 
        <p class="nolink"> 
            <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>"> 
        </p>
        <p class="nolink"> 
            <label for="file">File to upload:</label> 
            <input id="file" type="file" name="file"> 
        </p> 
        <p class="nolink"> 
            <label for="submit">Press to...</label> 
            <input id="submit" type="submit" name="submit" value="Upload me!"> 
        </p>
        <font size="-2">Max file size: <?php echo $max_file_size ?></font> 
    </form>

 

Why can't I upload any images? PHP itself returns no errors and doesn't upload... But my error handler returns errors.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/185491-php-image-uploader-not-working/
Share on other sites

This I presume is the code that is outputting the error message you are getting -

@imagepng($_FILES[$fieldname]['tmp_name'], $uploadFilename)
or error('receiving directory insuffiecient permission', $uploadForm); 

 

What does using the imagepng() function (with incorrect parameters as well) have to do with directory permissions? Did you even read the code that is producing the error so that you know the code is what you expect?

This I presume is the code that is outputting the error message you are getting -

@imagepng($_FILES[$fieldname]['tmp_name'], $uploadFilename)
or error('receiving directory insuffiecient permission', $uploadForm); 

 

What does using the imagepng() function (with incorrect parameters as well) have to do with directory permissions? Did you even read the code that is producing the error so that you know the code is what you expect?

 

I'm assuming imagepng() converts the given image to PNG then copies it...? Am I right?

if you echo out $uploadsDirectory, does it give you the expected result?

 

don't suppress your functions (get rid of @: @imagepng) until you are absolutely positive they work .. and even then, it's not necessary if proper error handling is in place.

 

and in your form, <?php $_SERVER['PHP_SELF'] ?> in the action is not doing anything.  view your source, you'll see.

 

if you wanted this to work, you need to echo it out.

 

you don't need that anyways.  if you are not calling a specific file, just leave that empty.

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.