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


What would be the path of the target folder while uploading image in 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.";
      }

我正试图通过网页上传图像…但是不行

请确保您的表单标签包含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."; }
?> 

您可以在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"]; }
?> 

为了更严格的检查…

<?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>
  • php.ini中启用file_uploads指令。