在php中上传图片时,目标文件夹的路径是什么?

时间:2016-08-08 08:04:13

标签: php

<?php 
      $target_dir ="uploads/";
      $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

      if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
      {
        //echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
      } else 
      {
         echo "Sorry, there was an error uploading your file.";
      }

我正在尝试通过网页上传图片...但它无效

3 个答案:

答案 0 :(得分:0)

plz确保您的表单标记包含enctype="multipart/form-data"属性 你可以试试这个没有$ _SERVER

<?php
$target_dir = "uploads/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded."; }
else { echo "Sorry, there was an error uploading your file."; }
?> 

答案 1 :(得分:0)

您可以在else语句中检查文件上传错误以查看错误

<?php
$target_dir = "uploads/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded."; }
else { echo "Sorry, there was an error uploading your file.". $_FILES["fileToUpload"]["error"]; }
?> 

答案 2 :(得分:0)

更严格的检查...

<?php

$target_dir = 'uploads/';

try {

    // Validate that the request is correctly sent from your HTML form.
    if (!isset($_FILES['fileToUpload']['error']) || !is_int($_FILES['fileToUpload']['error'])) {
        throw new RuntimeException('Insufficient parameters.', 400);
    }

    // Check errors.
    switch ($_FILES['fileToUpload']['error']) {
        case UPLOAD_ERR_OK:
            break;
        case UPLOAD_ERR_NO_FILE:
            throw new RuntimeException('File was not selected', 400);
        case UPLOAD_ERR_INI_SIZE:  // Over limit in php.ini
        case UPLOAD_ERR_FORM_SIZE: // Over limit in <input type="hidden" name="MAX_FILE_SIZE">
            throw new RuntimeException('Too large entity', 413);
        default:
            throw new RuntimeException('Unknown error', 500);
    }

    // You can alternatively check filesize here. 
    if ($_FILES['fileToUpload']['size'] > 1000000) {
        throw new RuntimeException('Too large entity', 413);
    }

    // Forbid remotely executable extensions, invisible or traversable filename
    if (!preg_match('/\A(?!\.)[\w.-]++(?<!\.php)(?<!\.cgi)\z/i', $_FILES['fileToUpload']['name'])) {
        throw new RuntimeException('Invalid filename', 400);
    } 

    // Move uploaded file
    if (!move_uploaded_file(
        $target_dir . $_FILES['fileToUpload']['tmp_name'],
        $target_dir . $_FILES['fileToUpload']['name']
    )) {
        throw new RuntimeException('Failed to save uploaded file', 500);
    }

    header('Content-Type: text/plain; charset=UTF-8');
    echo 'Successfully uploaded';

} catch (RuntimeException $e) {

    header('Content-Type: text/plain; charset=UTF-8', true, $e->getCode() ?: 500);
    echo $e->getMessage();

}

另外,别忘了......

  • 将属性enctype="multipart/form-data"设置为<form>
  • file_uploads中启用php.ini指令。