移动上传的文件php时出错

时间:2018-01-28 13:34:36

标签: php file file-upload

我尝试将图片加载添加到我的网站,但它给了我错误。这是PHP工作的结果:

  

C:\用户\管理员\应用程序数据\本地\ TEMP \ php9664.tmp   警告:move_uploaded_file(/img/collection/usbwebserver.exe):无法打开流:第26行的D:\ Home \ Volodya \ Projects \ Web \ CollectionSite \ site \ php \ collection.php中没有此类文件或目录

     

警告:move_uploaded_file():无法移动' C:\ Users \ Admin \ AppData \ Local \ Temp \ php9664.tmp' to' /img/collection/usbwebserver.exe'在第26行的D:\ Home \ Volodya \ Projects \ Web \ CollectionSite \ site \ php \ collection.php

她的代码: 表格:

echo ('
    <form action="collection.php" method="post" enctype="multipart/form-data">
        <legend>Add coin</legend>
        <select name="countryId">
');     

if(mysqli_num_rows($resultCountry)>0) {
    while($row=mysqli_fetch_assoc($resultCountry)) {
        echo("<option value=".$row['country_id'].">".$row['country_name']."</option>");
    }
}

echo('
        </select>
        <select name="curencyId">
');

if(mysqli_num_rows($resultCurency)>0) {
    while($row=mysqli_fetch_assoc($resultCurency)) {
        echo("
            <option value=".$row['curency_id'].">".$row['curency_name']."</option>
        ");
    }
}

echo('
    </select>
    <select name="metalId">
');

if(mysqli_num_rows($resultMetal)>0) {
    while($row=mysqli_fetch_assoc($resultMetal)) {
        echo("
            <option value=".$row['metal_id'].">".$row['metal_name']."</option>
        ");
    }
}

echo('
        </select>
        <select name="nominalId">
');

if(mysqli_num_rows($resultNominal)>0) {
    while($row=mysqli_fetch_assoc($resultNominal)) {
        echo("
            <option value=".$row['nominal_id'].">".$row['nominal_name']."</option>
        ");
    }
}

echo('
        </select>
        <input type="date" name="dateCreation">
        <input type="number" name="weight">
        <input type="number" name="mintage">
        <input type="file" name="img">
        <input type="hidden" name="type" value="addcoin">
        <input type="submit" value="Add coin">
    </form>               
');

和应对文件的代码:

echo($_FILES['img']['tmp_name']);
move_uploaded_file($_FILES['img']['tmp_name'], "/img/collection/".basename($_FILES['img']['name']));

1 个答案:

答案 0 :(得分:1)

您尝试将图像存储在磁盘的根目录中。

尝试使用__dir__$_SERVER['DOCUMENT_ROOT']

为文件路径添加前缀

例如:

move_uploaded_file($_FILES['img']['tmp_name'], 
    __dir__ . "/img/collection/".basename($_FILES['img']['name'])) ;

编辑在以下评论之后:

00000000  63 6f e2 80 8c e2 80 8b  6c 6c 65 63 74 69 6f 6e  |co......llection|
00000010  20 0a 63 6f e2 80 8c e2  80 8c e2 80 8b e2 80 8b  | .co............|
00000020  6c 6c 65 63 74 69 6f 6e                           |llection|