Обнаружена ошибка при загрузке файла с использованием php - PullRequest
0 голосов
/ 26 мая 2018

Ошибка в том, что я использую имя файла «файл» в html. И так же использую «файл» в файле php для доступа к файлу, но это не сработало. Здесь, ребята, я столкнулся с проблемойсвязанный файл загрузки в php Я думаю, мой код правильный, но файл не загружен на сервер Пожалуйста, помогите мне.

Html-код здесь

<form id="login-form"  action="welcome.php" class="form" method="post" witdth= "80px;" enctype="multipart/form-data"> 
<input type="file" class="" name="file" id="file" style="background: gray;">
            <br>
            <br>
            <div class="form-group">
                <button type="submit" id="register-btn" onClick="register();return false;"
                        class="btn btn-primary btn-block">Submit &nbsp; </button>

            </div>
        </form>

Php-код здесь

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES['file']["name"]);
$uploadOk = 1;
$File = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image

// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}

?>

Ответы [ 2 ]

0 голосов
/ 26 мая 2018

Вы только что определили имя файла, но не переместили загруженный файл на свой сервер.

//$target_file = $target_dir . basename($_FILES['file']["name"]);
$uploadOk = 1;
$File = strtolower(pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION));
if(in_array($File, ['png', 'jpg', 'jpeg', 'gif'])){
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_dir . basename($_FILES['file']['name']))){
//echo 'File uploaded !';
//all post manipulations here
}else{
echo 'Upload failed';
}
}else{
echo 'File is not image type';
}
0 голосов
/ 26 мая 2018
if (isset($_POST["submit"])) {
    if ($_FILES["file"]["name"] != "") {

//if there was an error uploading the file
        if ($_FILES["file"]["error"] > 0) {

            $msg = "Error while uploading file-Return Code: " . $_FILES["file"]["error"] . "<br />";
            $uploadedStatus = 0;
        } else {


            if (file_exists($_FILES["file"]["name"])) {
                unlink($_FILES["file"]["name"]);
            }

            $allowed_filetypes = array('.jpg', '.JPG', '.jpeg', '.JPEG');

            $filename = $_FILES['file']['name']; // Get the name of the file (including file extension).
            $ext = substr($filename, strpos($filename, '.'), strlen($filename) - 1); // Get the extension from the filename.

            if (!in_array($ext, $allowed_filetypes)) {
                $msg = "invalid file type";
                $uploadedStatus = 0;
            } else {
                $file_name = uniqid() . $ext;
                $storagename = "../assests/images/upload_images/" . $file_name;
                move_uploaded_file($_FILES["file"]["tmp_name"], $storagename);
                $uploadedStatus = 1;
            }
        }
    } else {
        $uploadedStatus = 1;
        $file_name = "";
    }

if ($uploadedStatus == 1) {

//your code
}
else{
$msg = "Error while uploading file-Return Code: " . $_FILES["file"]["error"] . "<br />";
}

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...