上传图片到服务器[英] Upload images on server

问题描述

<?php
  $target_path1 = "uploads/";
  
  /* Add the original filename to our target path.
  Result is "uploads/filename.extension" */
  
  $target_path1 = $target_path1 . basename( $_FILES['uploaded_file']['name']);
  if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path1)) 
  {
      echo "The first file ".  basename( $_FILES['uploaded_file']['name'])."has been uploaded.";
  } else {
      echo "There was an error uploading the file, please try again!";
      echo "filename: " .  basename( $_FILES['uploaded_file']['name']);
      echo "target_path: " .$target_path1;
  }
?>

这里有 3 个错误

<块引用>

注意:未定义索引:upload_file in D:\anddev\project\web\upload_test\upload_media_test.php 第 5 行

注意:未定义索引:upload_file in D:\anddev\project\web\upload_test\upload_media_test.php 第 6 行

注意:未定义索引:upload_file in D:\anddev\project\web\upload_test\upload_media_test.php 第 11 行

如何修复此代码?

推荐答案

始终添加检查以确保您确实得到了一些东西,

你可以利用 is_uploaded_file 试试看像这样:

<?php
  if(is_uploaded_file($_FILES['uploaded_file']['tmp_name']){
    //we got something, set it up
    $target_path1 = "uploads/";
    $file = basename( $_FILES['uploaded_file']['name']);        
    $full_path = $target_path1.$file; 
    //perform the upload
  if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $full_path)) {
      echo "The first file ".$file." has been uploaded.";
    } else {
      echo "There was an error uploading the file, please try again!";
      echo "filename: " .  $file;
    }
  }else{
      echo "Nothing was uploaded";
  }
?>

其他推荐答案

put:

<?php
var_dump($_FILES);
exit;

就在文件的开头并检查那里有什么.您的数组中有错误的键.检查输入文件的正确密钥是什么.

查看更多