0

我有一个允许文件上传的代码。当遇到 Show Error 4 UPLOAD_ERR_NO_FILE 时,它会抛出一个错误处理程序。function error并且error array它下面的一些代码对此负责。我想要的是消除该错误并在没有上传文件时使其正常。意思是,即使没有上传文件,代码仍会继续处理。我怎样才能做到这一点?这让我负担了很多天。请提供帮助,或将其分享给您认识的可以提供帮助的人。非常感谢您。

索引.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>

处理器.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

我尝试只删除功能错误,但在识别时它不能正常工作。希望你能帮助我。我真的需要让它发挥作用。提前感谢您帮助我解决问题。:)

4

0 回答 0