douqiao2008 2019-07-23 16:07
浏览 45

如何添加文本字段以在上载时更改文件名

I am uploading images and cropping to a directory, how do I add a text field to change the file name when uploading?

I have tried to add name field to the form but are unable to get the form to post the newname when uploading.

<form action="" method="POST" enctype="multipart/form-data">
<input id="input-6" name="art[]" type="file" multiple class="file- 
loading">
<input type="text" name="newname"/> 
<input type="submit" name="sub">

 </form>
<?php 
if(isset($_POST['sub'])){ (my guess is that "sub" needs to change to 
"newname" but that does not seem to work)
  if(isset($_FILES['art'])){
    foreach ($_FILES["art"]["error"] as $key => $error) {
      if ($error == UPLOAD_ERR_OK) {
       $tmp_name = $_FILES["art"]["tmp_name"][$key];
        $name = $_FILES["art"]["name"][$key];
        $image = imagecreatefromstring(file_get_contents(($tmp_name)));
          $filename = 'newimage/'.$name.'';

}
  ?>

NO error messages just doesn't append the file name.

  • 写回答

3条回答 默认 最新

  • dongyan4424 2019-07-23 16:17
    关注

    You are missing a whole part of the actual upload (most basic example involves using move_uploaded_file). Currently, your script is just asking for data (binary file and newname) and parsing them, without doing anything. Once you got the data, you need to upload the data. at that step, you can edit the filename.

    for example (taken from https://www.tutorialspoint.com/php/php_file_uploading, modified to include a new name field):

    <?php
       if(isset($_FILES['image'])){
          $errors= array();
          $file_name = $_POST['newname'] || $_FILES['image']['name'];
          $file_size =$_FILES['image']['size'];
          $file_tmp =$_FILES['image']['tmp_name'];
          $file_type=$_FILES['image']['type'];
          $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
    
          $extensions= array("jpeg","jpg","png");
    
          if(in_array($file_ext,$extensions)=== false){
             $errors[]="extension not allowed, please choose a JPEG or PNG file.";
          }
    
          if($file_size > 2097152){
             $errors[]='File size must be excately 2 MB';
          }
    
          if(empty($errors)==true){
             move_uploaded_file($file_tmp,"images/".$file_name);
             echo "Success";
          }else{
             print_r($errors);
          }
       }
    ?>
    <html>
       <body>
    
          <form action="" method="POST" enctype="multipart/form-data">
             <input type="file" name="image" />
             <input type="text" name="newname" />
             <input type="submit"/>
          </form>
    
       </body>
    </html>
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备