繁体   English   中英

PHP上传问题

[英]Problems with PHP upload

我的代码:

if(isset($_FILES['image'])){
    $allowedExts = array('jpg', 'gif', 'png');
    $extension = end(explode('.', $_FILES['image']['name']));
    if(in_array($extension, $allowedExts)){
        if($_FILES['image']['size'] < 50000){
            if ($_FILES['image']['error'] > 0){
                $uploaderror = $_FILES['image']['error'];
            }else{
                $uploaderror = 'FALLBACK ERROR';
                if(file_exists('..images/'.$_FILES['image']['name'])){
                    $uploaderror = 'The file <strong>'.$_FILES['image']['name'].'</strong> already exists in the images directory.';
                }else{
                    move_uploaded_file($_FILES['file']['tmp_name'], '..images/'.$_FILES['file']['name']);
                    $uploadsuccess = $_FILES['file']['name'];
                }
            }
        }else{$uploaderror = 'The image is too large.';}
    }else{$uploaderror = 'Only images (.jpg, .png, and .gif) are allowed.';}
}else{$uploaderror = 'No attempt';}



输出: $uploaderror返回FALLBACK ERROR并且未设置$uploadsuccess 该文件未出现在指定的目录中,并且在服务器上找不到该文件。 请告诉我我在做什么错。 谢谢!

您在..images之前缺少/ ,要解决此问题,只需更改以下内容:

move_uploaded_file($_FILES['file']['tmp_name'], '..images/'.$_FILES['file']['name']);

到以下内容:

move_uploaded_file($_FILES['file']['tmp_name'], '../images/'.$_FILES['file']['name']);

通过终端运行代码,您将得到以下响应:

..images:没有这样的文件或目录

编辑

我在file_exists()检查中找到了另一个忘记/

我也清理了您的代码,使其更具可读性:

<?php
$errors = array();
$allowedExts = array('jpg', 'gif', 'png');
$extension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
if(!isset($_FILES['image'])){
    $errors[] = "No attempt.";
}
if(in_array($extension, $allowedExts)){
    $errors[] = "Only images (.jpg, .png, and .gif) are allowed.";
}
if($_FILES['image']['size'] > 50000){
    $errors[] = "The image is too large.";
}
if ($_FILES['image']['error'] <= 0){
    $errors[] = $_FILES['image']['error'];
}
if(file_exists('../images/'.$_FILES['image']['name'])){
    $errors[] = 'The file <strong>'.$_FILES['image']['name'].'</strong> already exists in the images directory.';
}

// No errors found!
if(count($errors) == 0){
    move_uploaded_file($_FILES['file']['tmp_name'], '../images/'.$_FILES['file']['name']);
    $uploadsuccess = $_FILES['file']['name'];
}

暂无
暂无

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

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