修改上传的文件

时间:2016-11-14 15:58:26

标签: php file upload

所以我创建了一个文件上传网站,上传文件效果很好。 但问题是,稍后,当我想编辑文件(替换)时,文件不会像它应该那样更新。

我遇到问题,当我上传大写字母的文件时,我使用“strtolower”。当我想用“编辑”替换该文件时,“strtolower”似乎不起作用,我无法访问该文件。文件名显示在表格中,但是当我点击“查看”时,它没有获取文件,因此,它会上传到服务器。

以下是上传代码和edit.php。:

 if(isset($_POST['btn-upload']))
 {    

 $file = rand(1000,100000)."-".$_FILES['file']['name'];
 $file_loc = $_FILES['file']['tmp_name'];
 $file_size = $_FILES['file']['size'];
 $file_type = $_FILES['file']['type'];
 $first_name = $_POST['first_name'];
 $last_name = $_POST['last_name'];
 $user_city = $_POST['user_city'];
 $sifra = $_POST['sifra_izvoda'];
 $folder="uploads/";



 $new_size = $file_size/1024;  

 mb_internal_encoding('UTF-8');

 $new_file_name = mb_strtolower($file);


 $final_file = str_replace(' ','-',$new_file_name);

 if(move_uploaded_file($file_loc,$folder.$final_file))
 {
    $sql="INSERT INTO tbl_uploads(file,type,size,first_name,last_name,user_city,sifra) VALUES('$final_file','$file_type','$new_size','$first_name','$last_name','$user_city','$sifra')";
    mysql_query($sql);
    ?>
    <script>
    alert('Izvod je dodan');
    window.location.href='view.php?success';
    </script>
    <?php
}

这里是我想编辑上传文件时的代码:

 if(isset($_GET['edit_id']))
 {
 $sql_query="SELECT file, type, size, first_name, last_name, user_city,   sifra  FROM tbl_uploads WHERE id=".$_GET['edit_id'];
 $result_set=mysql_query($sql_query);
 $fetched_row=mysql_fetch_array($result_set);
}
if(isset($_POST['btn-update']))
{
  $file = rand(1000,100000)."-".$_FILES['file']['name'];
  $file_loc = $_FILES['file']['tmp_name'];
  $file_size = $_FILES['file']['size'];
  $file_type = $_FILES['file']['type'];
  $first_name = $_POST['first_name'];
  $last_name = $_POST['last_name'];
  $user_city = $_POST['user_city'];
  $sifra = $_POST['sifra_izvoda'];
  $folder="uploads/";

  $new_size = $file_size/1024;  

  mb_internal_encoding('UTF-8');

  $new_file_name = mb_strtolower($file);


  $final_file = str_replace(' ','-',$new_file_name);

  $tmp = move_uploaded_file($file_loc,$folder.$final_file);

  $insert_file = ($_FILES['file']['name'])?"file='$file', type='$file_type',     size='$file_size',":'';

 $sql_query = "UPDATE tbl_uploads SET $insert_file  first_name='$first_name', last_name='$last_name', user_city='$user_city', sifra='$sifra' WHERE id=".$_GET['edit_id'];


 if(mysql_query($sql_query))
 {
   header("Location: view.php");
 ?>

1 个答案:

答案 0 :(得分:0)

好的解决方案是这样的:

$new_size = $file_size/1024;  

  mb_internal_encoding('UTF-8');
  $new_file_name = mb_strtolower($file);
  $final_file = str_replace(' ','-',$new_file_name);
  $tmp = move_uploaded_file($file_loc,$folder.$final_file);

  $insert_file = ($_FILES['file']['name'])?"file='$final_file', type='$file_type', size='$file_size',":'';


 $sql_query = "UPDATE tbl_uploads SET $insert_file  first_name='$first_name', last_name='$last_name', user_city='$user_city', sifra='$sifra' WHERE id=".$_GET['edit_id'];

我在查询中添加了 file ='$ final_file ,现在它正确更新