duanpan3166 2017-01-20 13:34
浏览 87
已采纳

POST返回文件的空值

I am trying to upload an image using php and pdo. Here is my form

<form action="action.php" class="w3-container" method="post" enctype="multipart/form-data">
 <p>  <label>Image Upload</label>
     <input type="file" name="st_im"></p>
 <input type="submit" value="Submit" class="w3-btn w3-blue">
<input type ="hidden" name="submitted" value="TRUE">

And here's is my php file

if (isset($_POST['submitted'])) {
$errors = array(); 

$imgFile = $_FILES['st_im']['name'];
$tmp_dir = $_FILES['st_im']['tmp_name'];
$imgSize = $_FILES['st_im']['size'];
if (empty($_POST['st_im'])) {
$errors[] = 'you forgot the picture!';
}else{

$upload_dir = 'uploads/'; // upload directory

$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension

 // valid image extensions
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions

 // rename uploading image
$st_im = rand(1000,1000000).".".$imgExt;
}
 if(in_array($imgExt, $valid_extensions)){
// Check file size '5MB'
 if($imgSize < 5000000)    {
 move_uploaded_file($tmp_dir,$upload_dir.$st_im);
 }
 else{
 $errors[] = "Sorry, your file is too large.";
 }
 }
 else{
 $errors[] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
 }
 if (empty($errors)){
 $stmt1=$conn->prepare("INSERT INTO magazia (st_img) VALUES (:st_im)");
 $stmt1->bindParam(':st_im', $st_im, PDO::PARAM_STR);
 $st_im= $st_im;
 $stmt1->execute();

After submiting the form I get the error: "you forgot the picture" which means that the (st_im) is null. I am stack here for about 2 hours

What I am missing?

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dongtang1910 2017-01-20 13:40
    关注

    There is no $_POST['st_im']. As you have correctly used in the lines before that, the information about the upload is stored in $_FILES['st_im'].

    So you should check for this instead:

    if ($_FILES['st_im']['error'] != UPLOAD_ERR_OK) {
       // Upload error
       $errors[] = 'you forgot the picture!';
    }else{
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!