问题描述:

I have a code which allows file upload. It throws an error handler when it encounters Show Error 4 UPLOAD_ERR_NO_FILE. function error and error array and some code below it is responsible for that. what i want is to ELIMINATE THAT ERROR AND MAKE IT JUST FINE WHEN NO FILE IS UPLOADED. meaning, the code still continues to process even with no uploaded file. How can I do that? it burdened me for many days now. please extend a help, or share this to someone you know can help. thank you very much.

Index.php

 <?php

// make a note of the current working directory relative to root.

$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

// make a note of the location of the upload handler

$uploadHandler = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'processor.php';

// set a max file size for the html upload form

$max_file_size = 30000; // size in bytes

// now echo the html page

?>

<!DOCTYPE html>

<html class="html">

<head>

<div class="container">

<form action="<?php echo $uploadHandler ?>" enctype="multipart/form-data" method="post">

SOME HTML CODE HERE

</form>

</div>

</head>

</html>

Processor.php

$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');

// check the upload form was actually submitted else print form

isset($_POST['submit'])

or error('the upload form is neaded', $uploadForm);

// check for standard uploading errors

($_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);

$now = time();

while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))

{

$now++;

}

// now let's move the file to its final and allocate it with the new filename

@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)

or error('receiving directory insuffiecient permission', $uploadForm);

$name = $_POST['name'];

$email = $_POST['email'];

$office_id = $_POST['office_id'];

$get_title = $_POST['title'];

$title = strtoupper ($get_title);

$namehere = "Super Story By " .$_POST['name'];

$story = $_POST['story'];

header('Content-Type: image/jpeg');

$upload = $uploadFilename;

$im = imagecreatefromjpeg("bg.jpg");

$img2 = imagecreatefromjpeg($upload);

$black = imagecolorallocate($im, 0, 0, 0);

$font = 'arialbi.ttf';

$font2 = 'ariali.ttf';

/*SOME MORE IMAGETTFTEXT CODE HERE*/

imagedestroy($im);

if (!empty($name) && !empty($email) && !empty($office_id) && !empty($title) && !empty($story)) {

SOME QUERY HERE

}

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;

} // end error handler

I tried just deleting the function error but it's not working well when i d that. hope you can help me with this. I really need to make it work. Thank you in advance for helping me sort it out. :)

相关阅读:
Top