$ _FILE不会在php中发送文件

时间:2019-05-19 14:35:07

标签: php file-upload

问题是我创建了以下表单:

<form action="" method="get">
	<div class="select">
		<input name="u" type="radio" value="meme" class="memeSelect" <?php echo $selectMeme ?>>
		<label for="meme">Meme</label>
		<input name="u" type="radio" value="plantilla" class="plantillaSelect" <?php echo $selectPlantilla ?>>
		<label for="plantilla">Plantilla</label>
		<input type="submit" class="invisible selectMemePlantilla">
	</div>
</form>

<form enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
	<div class="meme <?php echo $invisibleMeme ?>">
		<input type="hidden" name="MAX_FILE_SIZE" value="20000"/>
		<input type="file" class="file" name="file" accept="image/png, image/jpeg, image/gif, image/pjpeg">
		<h2>Elije las clases para el meme</h2>
		<div class="clases">
			<input type="checkbox">
			<input type="checkbox">
			<input type="checkbox">
			<input type="checkbox">
		</div>
		<span><?php echo $error ?></span>
		<input type="submit" name="uploadBtn" value="Upload">
	</div>
	<div class="plantilla <?php echo $invisiblePlantilla ?>">
		<input type="hidden" name="MAX_FILE_SIZE" value="20000"/>
		<input type="file" class="file" name="file" accept="image/png, image/jpeg, image/gif, image/pjpeg">
		<h2>Elije las clases para la plantilla</h2>
		<div class="clases">
			<input type="checkbox">
			<input type="checkbox">
			<input type="checkbox">
			<input type="checkbox">
		</div>
		<span><?php echo $error ?></span>
		<input type="submit" name="uploadBtn" value="Upload">
	</div>
</form>

您可以看到要上传图像的页面,首先要有一个获取表单,可以转到要上传模因或模板的页面,然后要有一个帖子可以将图像上传到服务器。

第一种形式可以正常工作,但是第二种形式却不能,我总是跳过错误4,即没有文件上传。

我创建了另一个文件,以查看服务器是否有问题或什么问题,但一切正常。问题一定在于尝试使用get,但是我不知道如何解决它。

用于上传图片的PHP表单:

if (isset($_POST['uploadBtn']) && $_POST['uploadBtn'] == 'Upload') {
    if (isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK) {

        $fileTmpPath           = $_FILES['file']['tmp_name'];
        $fileName              = $_FILES['file']['name'];
        $fileSize              = $_FILES['file']['size'];
        $fileType              = $_FILES['file']['type'];
        $fileNameCmps          = explode(".", $fileName);
        $fileExtension         = strtolower(end($fileNameCmps));
        $newFileName           = md5(time() . $fileName) . '.' . $fileExtension;
        $allowedfileExtensions = array('jpg', 'gif', 'png', 'zip', 'txt', 'xls', 'doc');
        echo $fileName;

        if (in_array($fileExtension, $allowedfileExtensions)) {
            $uploadFileDir = 'img/';
            $dest_path = $uploadFileDir . $newFileName;
            echo "vamoos";

            if(move_uploaded_file($fileTmpPath, $dest_path)) {
                $message ='File is successfully uploaded.';
                $_SESSION['message'] = $message;
                echo $message;
            }
            else {
                $message = 'There was some error moving the file to upload directory. Please make sure the upload directory is writable by web server.';
                $_SESSION['message'] = $message;
                echo "3";
            }
        }
    }else {echo "algo no funciona1"; }
}else {echo "algo no funciona"; } 

此PHP用于获取表单:

$selectMeme = $selectPlantilla = "";
    $invisibleMeme = $invisiblePlantilla = "invisible";
    $error = $selected = "";
    if ( isset($_GET['u']) ) {

        if ( $_GET['u'] == "meme" ) {
            $selected           = "memes";
            $selectMeme         = "checked";
            $invisibleMeme      = "";
            $selectPlantilla    = "";
            $invisiblePlantilla = "invisible";

        } else {
            $selected           = "plantillas";
            $selectPlantilla    = "checked";
            $invisiblePlantilla = "";
            $selectMeme         = "";
            $invisibleMeme      = "invisible";
        }
    }

1 个答案:

答案 0 :(得分:0)

此错误可能有两个原因:

  1. 您正在为plantillameme这两种格式使用相同的名称。您应该使用不同的文件名字段,因为它们都具有相同的名称file
  2. move_uploaded_file需要目的地的绝对路径。您应该使用完整的路径来使其正常工作。

谢谢,希望对您有帮助!