将图像上传到站点

时间:2012-12-31 21:51:17

标签: php html

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

$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():无法在C:\ xampp \ xxxxxxxxx \ xxx \ xx \ x中将'C:\ xampp \ xxxxx \ php3D71.tmp'移动到'C:\ xampp \ xxx \ xxx \ thumbnails1'第117行的.php

我无法弄清楚出了什么问题。表格是enctype =“multipart / form-data”有什么建议吗?谢谢。

1 个答案:

答案 0 :(得分: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文件夹。仔细检查目标路径/目录。确保它绝对正确。

另一个可能的问题是我在/&amp;之间没有看到$_PARAM["ThumbnailDir"]$sFileName表示文件夹thumbnails ~ ($_PARAM["ThumbnailDir"])。它可以像改为 -

一样简单
$sTargetLink = $_PARAM["ThumbnailDir"] .'/'.$sFileName;