繁体   English   中英

用PHP上传文件的问题

[英]Problems with uploading files with PHP

我目前的程式码(无法运作):

<label for="homepage"><h3>Home Page Image</h3></label><input type="hidden" name="MAX_FILE_SIZE" value="300000" /><input type="file" name="homepage" />
        <?php
            $tmp_name = $_FILES['homepage']['tmp_name'];
            $file_type= $_FILES['homepage']['type'];
            $name = strtolower($_POST['title']) . ".png";
            $upload_dir = "/slideshow/";
            $file_path = "$upload_dir/$name";
            list($width, $height, $type, $attr)=getimagesize("$tmp_name");

            if(isset($_POST['submit']) && $file_type == "image/png" && $width==800 && $height==250)
            {
                move_uploaded_file($tmp_name, "$upload_dir/$name");
            }
            elseif((isset($_POST['submit']) && ($file_type != "image/png" || $width!=800 || $height!=250)))
            {
                echo "<div class=\"errorcheck\">Please select an image that meets the requirements. </div>";
                echo "$width $height $type";
            }
        ?>
</br>

返回“警告:move_uploaded_file(slideshow / test.png)[function.move-uploaded-file]:无法打开流:权限在第93行的/home/xxx/xxx/back_end/add.php中被拒绝警告:move_uploaded_file() [function.move-uploaded-file]:无法在第93行的/home/xxx/xxx/back_end/add.php中将'/ tmp / phpGXQv9g'移动到'slideshow / test.png'

我是初学者,因此请以简单的方式进行说明,谢谢。

这里的问题(我认为)是您引用$ tmp_name变量的方式。

它不应该用双引号引起来。 还是应该像这样

...getimagesize("${tmp_name}");

您几乎没有错误处理,请遵循php.net上的建议:

http://www.php.net/manual/zh/features.file-upload.post-method.php

首先,要进行调试,至少要打印出$_FILES数组:

echo 'Here is some more debugging info:';
print_r($_FILES);

您似乎在尝试回显并在函数内使用变量时可能会出现很多错误。 这样做并在引号内使用$ variable调用它们时,您会将其编码为设置的字符串而不是变量。 例如:

$foo = 'bar';
echo $foo;
Result: bar

$foo = 'bar';
echo '$foo';
Result: $foo

尝试更改此:

$name = strtolower($_POST['title']) . ".png";
$upload_dir = "/slideshow/";
$file_path = "$upload_dir/$name";

对此:

$file_path = $upload_dir."/".$name;

move_uploaded_file($tmp_name, "$upload_dir/$name");

对此:

move_uploaded_file($tmp_name, $file_path);

当您使用标签时,请不要忘记标签...否则它将不会提交POST方法的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM