php使用move_uploaded_file函数上传文件报错failed to open stream: Invalid argument in解决方案

php使用move_uploaded_file上传文件报错failed to open stream: Invalid argument in 解决方案

先报错failed to open stream: Invalid argument

然后写入失败。

文档编码使用UTF-8

因为用英文名称文件上传,一切正常

代码如下:

<?php
/*
UploadiFive
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
*/

// Set the uplaod directory
$uploadDir = 'uploadify/uploads/';

// Set the allowed file extensions
$fileTypes = array('jpg', 'jpeg', 'gif', 'png'); // Allowed file extensions

$verifyToken = md5('unique_salt' . $_POST['timestamp']);

if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $uploadDir = $_SERVER['DOCUMENT_ROOT'] . $uploadDir;
    $targetFile = $uploadDir . $_FILES['Filedata']['name'];

	//关键性代码 转码
    if (function_exists("iconv"))     {
        $targetFile=iconv("UTF-8","GBK",$targetFile);
    }


    echo $targetFile;
    // Validate the filetype
    $fileParts = pathinfo($_FILES['Filedata']['name']);
    if (in_array(strtolower($fileParts['extension']), $fileTypes)) {

        // Save the file
        move_uploaded_file($tempFile, $targetFile);
        echo 1;

    } else {

        // The file type wasn't allowed
        echo 'Invalid file type.';

    }
}
?>

在这里插入图片描述

在这里插入图片描述

当在 DVWA 文件上传时 `move_uploaded_file` 函数报错 `failed to open stream: Invalid argument`,可从以下几个方面排查并解决问题: ### 1. 检查文件路径 `move_uploaded_file` 函数需要一个有效的目标路径。若路径包含无效字符、不存在的目录或者权限不足,就会出现此错误。 ```php // 原始代码示例 $uploaded_file = $_FILES['uploaded']['tmp_name']; $target_path = $_SERVER['DOCUMENT_ROOT'] . '/DVWA/vulnerabilities/upload/uploads/' . $_FILES['uploaded']['name']; // 确认目标目录是否存在 if (!is_dir(dirname($target_path))) { mkdir(dirname($target_path), 0777, true); } // 检查目标路径是否有效 if (is_uploaded_file($uploaded_file)) { if (move_uploaded_file($uploaded_file, $target_path)) { echo "文件上传成功!"; } else { echo "文件上传失败!"; } } ``` 在上述代码中,先确认目标目录是否存在,若不存在则创建该目录,以此保证目标路径有效。 ### 2. 检查文件上传临时目录 PHP 的临时上传目录可能未正确配置或者没有写入权限。可通过 `php.ini` 文件查看和修改临时上传目录。 ```ini ; php.ini 配置 upload_tmp_dir = "C:/phpstudy/temp" ; 修改为有效且有写入权限的目录 ``` 修改完成后,重启 Web 服务器和 PHP-FPM 使配置生效。 ### 3. 检查文件上传大小限制 若上传文件大小超过了 `php.ini` 中配置的限制,`move_uploaded_file` 也可能会失败。 ```ini ; php.ini 配置 upload_max_filesize = 10M ; 增大上传文件大小限制 post_max_size = 10M ; 增大 POST 请求大小限制 ``` 修改完成后,重启 Web 服务器和 PHP-FPM。 ### 4. 检查文件上传表单 确保 HTML 表单的 `enctype` 属性设置为 `multipart/form-data`,且 `method` 属性设置为 `post`。 ```html <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="uploaded"> <input type="submit" value="上传文件"> </form> ``` ### 5. 检查文件权限 确保 Web 服务器对上传目录和临时目录有读写权限。在 Windows 系统中,要保证 IIS 或 Apache 服务账户对相关目录有足够的权限;在 Linux 系统中,可通过 `chmod` 和 `chown` 命令修改文件和目录的权限和所有者。 ```bash # Linux 系统示例 chmod -R 777 /var/www/html/DVWA/vulnerabilities/upload/uploads ``` ### 6. 检查文件上传字段名 确保 PHP 代码中使用文件上传字段名和 HTML 表单中的 `name` 属性一致。 ```php // PHP 代码 $uploaded_file = $_FILES['uploaded']['tmp_name']; // 确保 'uploaded' 和 HTML 表单中的 name 属性一致 ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值