消除错误4 UPLOAD_ERR_NO_FILE /不要

时间:2013-11-05 02:22:52

标签: php

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

的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

我尝试删除功能错误但是当我这样做时效果不佳。希望你能帮助我。我真的需要让它发挥作用。提前感谢您帮助我解决问题。 :)

0 个答案:

没有答案