简体   繁体   English

PHP move_uploaded_file到目录并创建文件

[英]PHP move_uploaded_file to directory and create file

I have a script that uploads a file and then moves it to a directory. 我有一个脚本,可以上传文件,然后将其移动到目录。 However the script does not know the name of the file its creating because it hasn't created it yet and cannot find the file to update. 但是,脚本不知道其创建文件的名称,因为它尚未创建文件,因此找不到要更新的文件。

So either one requires a way to make the file first or there is another way of doing this. 因此,要么需要一种首先制作文件的方法,要么存在另一种方法。 The code. 编码。

<?php

$filename = '/home/divethe1/public_html/update/z-images/admin/upload/test/';

if ($_FILES['thumbfile']['error'] === UPLOAD_ERR_OK) {
    $info = getimagesize($_FILES['thumbfile']['tmp_name']);
    if (($info[2] !== IMG_GIF) && ($info[2] !== IMG_JPEG)) {
       die("not a gif/jpg");
    }
    if (filesize($_FILES['thumbfile']['tmp_name']) > 100000) {
       die("larger than 100000");
    }
    move_uploaded_file($_FILES['thumbfile']['tmp_name'], $filename . $_FILES['thumbfile']['name']);

      echo '<script type="text/javascript">
parent.document.getElementById("thumbprogress").innerHTML = "Archiving"</script>Archiving';

  }
else
  {
  echo '<script type="text/javascript">
parent.document.getElementById("thumbprogress").innerHTML = "Invalid File Format"</script>Invalid File Format';
  }
?>

Any ideas? 有任何想法吗?

I think you're misunderstanding how move_uploaded_file() works. 我认为您误会了move_uploaded_file()的工作方式。 It doesn't create a file for you. 它不会为您创建文件。 It: 它:

  1. Takes the temporary filethat PHP created for you to hold the upload (the filename/path for which is in $_FILES['thumbfile']['tmp_name'] ) 取得PHP为您创建的临时文件以保存上载(文件名/路径位于$_FILES['thumbfile']['tmp_name']
  2. does a few security checks to make sure no one's tampered with the file between the time the upload completed and the move_uploaded_file call was issued 在上传完成和发出move_uploaded_file调用之间进行一些安全检查,以确保没有人篡改文件
  3. then MOVES the file to the location you specify. 然后将文件移动到您指定的位置。

It doesn't handle the upload, or receive the file - by the time your upload-handling script gets fired up, the upload has already been completed and the file is waiting in that tmp_name location. 它不会处理上载或接收文件-在启动上载处理脚本时,上载已经完成并且文件正在该tmp_name位置等待。

If the move can't be completed for any reason, move_uploaded_file() returns false. 如果由于某种原因无法完成移动,则move_uploaded_file()返回false。 It won't warn you if you're overwriting a file in the destination, on the assumption that you know what you're doing. 假设您知道自己在做什么,它不会在目标中覆盖文件时警告您。

My mistake. 我的错。 I left the directory test in place. 我将目录测试保留在原位。 That should have gone. 那应该过去了。 Thanks anyway for all help. 无论如何,谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM