1

我正在努力让用户将自己的缩略图上传到我的网站。我的代码是

$allowedExts = array("jpg", "jpeg", "gif", "png");
        $explode = explode(".", $_FILES["file"]["name"]);
        $extension = end($explode);
        if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/pjpeg") ||   ($_FILES["file"]["type"] == "images/jpg")) && ($_FILES["file"]["size"] < 50000) && in_array($extension, $allowedExts))
        {
            $sDate = date("m-d-Y");
            $sRandString = randomString(50);
            $sFileName = rename($_FILES["file"]["tmp_name"], $sDate . $sUsername . $sRandString);   //The name of the file
            $sTargetLink = $_PARAM["ThumbnailDir"] . $sFileName;
        
            if ($_FILES["file"]["error"] > 0)
                echo "There has been an unexpected error, we're working on fixing it as soon as possible. Please try again later.";
            else
            {
                if (file_exists($_PARAM["ThumbnailDir"] . $sFileName))
                {
                    unlink($_PARAM["ThumbnailDir"] . $sFileName);
                    $sFileLocation = $_PARAM["WebsiteAddress"]."images/thumbnails/".$sFileName; //The location of the file
                    move_uploaded_file($_FILES["file"]["tmp_name"],$_PARAM["ThumbnailDir"] . $sFileName);
                    echo "The thumbnail has been successfully uploaded.";
                    unlink($_FILES["file"]["tmp_name"]);
                }
                else
                {
                    $sFileLocation = $_PARAM["WebsiteAddress"] . "images/thumbnails/" . $sFileName; //The location of the file
                    $move = move_uploaded_file($_FILES["file"]["tmp_name"], $sTargetLink);
                    if ($move) echo "The thumbnail has been successfully uploaded.";
                    unlink($_FILES["file"]["tmp_name"]);
                }
            }
        }

然后链接位置将保存在数据库中,允许可检索到图像的链接。但是,无论何时提交,页面都会返回:

警告:move_uploaded_file(C:\xampp\xxxxx\php3D71.tmp):打开流失败:第 117 行的 C:\xampp\xxxxxxx\xxxxx\xxxx.php 中没有这样的文件或目录

警告:move_uploaded_file(): Unable to move 'C:\xampp\xxxxx\php3D71.tmp' to 'C:\xampp\xxx\xxx\thumbnails1' in C:\xampp\xxxxxxxxx\xxx\xx\x.php on第 117 行

我不知道出了什么问题。表格是 enctype="multipart/form-data" 有什么建议吗?谢谢你。

4

1 回答 1

1

问题 1 -
尝试更改为 -

$sFileName = $sDate . $sUsername . $sRandString;
rename($_FILES["file"]["tmp_name"], $sFileName);   //The name of the file
$sTargetLink = $_PARAM["ThumbnailDir"] . $sFileName;

因为rename()返回true,这条线 -

$sTargetLink = $_PARAM["ThumbnailDir"] . $sFileName;

正变成

$sTargetLink = 'thumbnails' . 1; //  => thumbnails1

问题 2 -
您的move_uploaded_file()警告/错误是因为它找不到您的thumbnails文件夹。仔细检查您的目标路径/目录。确保它是绝对正确的。

另一个可能的问题是我看不到/between $_PARAM["ThumbnailDir"]&$sFileName来表示文件夹thumbnails ~ ($_PARAM["ThumbnailDir"])。它可以像更改为一样简单 -

$sTargetLink = $_PARAM["ThumbnailDir"] .'/'.$sFileName;
于 2012-12-31T22:20:36.003 回答