使用 PHP 上传文件

三木力木

好的,所以我的文件上传遇到了一些问题,出于某种原因,它说我使用了无效的文件类型(我正在尝试上传一个简单的 JPEG 图片)

这是我为它制作的 HMTL

<div id="popup_FileUpload_bg">
    <div id="inner_fillUpload">
        <form action="image-upload.php" method="POST" enctype="multipart/form-data">
            <label>Tekst na slici</label><br/>
            <input id="txt_uploadCustom" name="customFileText" type="text" placeholder="Text..." /><br/><br/>

            <label>Tip postera</label><br/>
            <select name="selectVal">
                <option value="on_pic">Na slici</option>
                <option value="bellow_pic">Ispod slike</option>
                <option value="above_pic">Iznad slike</option>
                <option value="bellow_and_above">Iznad i ispod slike</option>
            </select><br/><br/>

            <label>Kliknite ispod da bi ste ubacili poster</label><br/>
            <input type="file" name="fileToUpload" id="fileToUpload" required><br/><br/>
            <input id="submitFileUploadCustom" type="submit" value="Postavite poster" name="submit">
        </form>
    </div>
</div>

和 PHP 本身

require_once "assets/core/user.php";

use app\user\control as user_c;

if(session_status() == PHP_SESSION_NONE) {
    session_start();
}
$fileNAME = substr(hash('sha512',basename($_FILES["fileToUpload"]["name"])."_".time()),0,10); //I think that this is the problem...
$target_dir = "assets/img/user_uploads/";
$target_file = $target_dir . $fileNAME;
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Fajl je preveliki";
    $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Nedozvoljeni tip fajla, mozete ubaciti JPG, JPEG, PNG & GIF kao poster sliku";
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "<br/>Sorry, your file was not uploaded. "."FILE NAME: ".$fileNAME."<br/> FILE TYPE: ".$imageFileType." <br/> TARGET: ".$target_file;
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]['tmp_name'], "assets/img/profiles/".$_SESSION['username'].".".$imageFileType)) {

        //Just adding stuff to MySQL
        $user = new user_c();

        $user->submitUserUpload($_SESSION['username'], $fileNAME, $_POST['customFileText'], $_POST['selectVal'], $imageFileType);

    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

我试过调试结果,这就是我得到的结果 抱歉,您的文件没有上传。

文件名称:58a0fc9cc0 文件类型:目标:assets/img/user_uploads/58a0fc9cc0

米哈尔 G.

看着

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" )

然后看看

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

然后

$target_file = $target_dir . $fileNAME;

而 $filename 是

$fileNAME = substr(hash('sha512',basename($_FILES["fileToUpload"]["name"])."_".time()),0,10); //I think that this is the problem...

所以......在“if”被触发的那一刻,文件名没有扩展名,所以“if”总是给你 false 。

尝试改变

$imageFileType = pathinfo($_FILES["fileToUpload"]["name"],PATHINFO_EXTENSION);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章