is_uploaded_file 数组给出错误

发布于 2024-11-04 13:25:15 字数 2610 浏览 0 评论 0原文

我的上传文件中出现此错误。语法是正确的,问题出在哪里?

错误:

is_uploaded_file() 期望参数 1 为字符串,数组在 C:\Program Files\EasyPHP-5.3.5.0\www\bena_website\admin\variables\upload_img.php 第 34 行给出

<h3>Please Choose a File and click Submit</h3>

        <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
            <input name="userfile[]" type="file" />
            <input type="submit" value="Submit" />
        </form>

<?php
        // check if a file was submitted
        if(!isset($_FILES['userfile'])) {
            echo '<p>Please select a file</p>';
        }
        else
            {
            print_r( $_FILES );
            try {
                upload();
                // give praise and thanks to the php gods
                echo '<p>Thank you for submitting</p>';
            }
            catch(Exception $e) {
                echo $e->getMessage();
                echo 'Sorry, could not upload file';
            }
        }
// the upload function
function upload(){

if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {

    // check the file is less than the maximum file size
    if($_FILES['userfile']['size'] < $maxsize)
        {
    // prepare the image for insertion
    $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
    // $imgData = addslashes($_FILES['userfile']);

    // get the image info..
      $size = getimagesize($_FILES['userfile']['tmp_name']);

    // put the image in the db...
      // database connection
      mysql_connect("localhost", "root", "") OR DIE (mysql_error());

      // select the db
      mysql_select_db ("bena") OR DIE ("Unable to select db".mysql_error());

    // our sql query
    $sql = "INSERT INTO testblob
            ( image_id , image_type ,image, image_size, image_name)
            VALUES
            ('', '{$size['mime']}', '{$imgData}', '{$size[3]}', '{$_FILES['userfile']['name']}')";

    // insert the image
    if(!mysql_query($sql)) {
        echo 'Unable to upload file';
        }
    }
}
else {
     // if the file is not less than the maximum allowed, print an error
     echo
      '<div>File exceeds the Maximum File limit</div>
      <div>Maximum File limit is '.$maxsize.'</div>
      <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].' bytes</div>
      <hr />';
     }
}
?>

i have this error in my upload file. The syntax is correct, where is the problem?

The error:

is_uploaded_file() expects parameter 1 to be string, array given in C:\Program Files\EasyPHP-5.3.5.0\www\bena_website\admin\variables\upload_img.php on line 34

<h3>Please Choose a File and click Submit</h3>

        <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
            <input name="userfile[]" type="file" />
            <input type="submit" value="Submit" />
        </form>

<?php
        // check if a file was submitted
        if(!isset($_FILES['userfile'])) {
            echo '<p>Please select a file</p>';
        }
        else
            {
            print_r( $_FILES );
            try {
                upload();
                // give praise and thanks to the php gods
                echo '<p>Thank you for submitting</p>';
            }
            catch(Exception $e) {
                echo $e->getMessage();
                echo 'Sorry, could not upload file';
            }
        }
// the upload function
function upload(){

if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {

    // check the file is less than the maximum file size
    if($_FILES['userfile']['size'] < $maxsize)
        {
    // prepare the image for insertion
    $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
    // $imgData = addslashes($_FILES['userfile']);

    // get the image info..
      $size = getimagesize($_FILES['userfile']['tmp_name']);

    // put the image in the db...
      // database connection
      mysql_connect("localhost", "root", "") OR DIE (mysql_error());

      // select the db
      mysql_select_db ("bena") OR DIE ("Unable to select db".mysql_error());

    // our sql query
    $sql = "INSERT INTO testblob
            ( image_id , image_type ,image, image_size, image_name)
            VALUES
            ('', '{$size['mime']}', '{$imgData}', '{$size[3]}', '{$_FILES['userfile']['name']}')";

    // insert the image
    if(!mysql_query($sql)) {
        echo 'Unable to upload file';
        }
    }
}
else {
     // if the file is not less than the maximum allowed, print an error
     echo
      '<div>File exceeds the Maximum File limit</div>
      <div>Maximum File limit is '.$maxsize.'</div>
      <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].' bytes</div>
      <hr />';
     }
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾听心声的旋律 2024-11-11 13:25:15

在 upload() 函数开始时发出 $_FILES 数组的结构,以了解该结构内部的内容:

print_r( $_FILES );

如注释之一所写,如果您有类似的表单

<form ... method="post" enctype="multipart/form-data">
   <input type="file" name="userfile[0]" />
   <input type="file" name="userfile[1]" />
   ...
</form>

,或者

<form ... method="post" enctype="multipart/form-data">>
   <input type="file" name="userfile[]" />
   <input type="file" name="userfile[]" />
   ...
</form>

访问:

$_FILES[ 'userfile' ][ 'tmp_name' ]

您需要像数组一样

$_FILES['userfile'][ 'tmp_name' ][ 0 ]
$_FILES['userfile'][ 'tmp_name' ][ 1 ]

Emit at the start of the upload() function the structure of the $_FILES array to learn, what's inside the structure:

print_r( $_FILES );

As written in one of the comments, if you have a form like

<form ... method="post" enctype="multipart/form-data">
   <input type="file" name="userfile[0]" />
   <input type="file" name="userfile[1]" />
   ...
</form>

or

<form ... method="post" enctype="multipart/form-data">>
   <input type="file" name="userfile[]" />
   <input type="file" name="userfile[]" />
   ...
</form>

you need the access this

$_FILES[ 'userfile' ][ 'tmp_name' ]

like an array:

$_FILES['userfile'][ 'tmp_name' ][ 0 ]
$_FILES['userfile'][ 'tmp_name' ][ 1 ]
≈。彩虹 2024-11-11 13:25:15

您还可以检查 $_FILES['userfile']['error'] 是否不为零。
例如,如果它是 2,则文件大小大于 HTML 中设置的大小。
有关错误消息的更多信息,请检查 http://php.net/manual/ en/features.file-upload.errors.php

You can also check to see if $_FILES['userfile']['error'] is different from zero.
If it's 2, for instance, then the file size is larger than the size set in HTML.
For more info on error messages check http://php.net/manual/en/features.file-upload.errors.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文