Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP文件上传_Php_Forms - Fatal编程技术网

PHP文件上传

PHP文件上传,php,forms,Php,Forms,如果我想在文件名转到服务器的永久位置(而不是临时位置)之前更改文件名,我该怎么做 代码如下: <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input

如果我想在文件名转到服务器的永久位置(而不是临时位置)之前更改文件名,我该怎么做

代码如下:

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>


<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

文件名:


您可以在“移动上传的文件”功能中执行此操作

move_uploaded_file($temporary_file, "path/to/destination/and/new_file_name.gif");

现在,您只需使用当前名称将其移动到目的地

我不确定我是否理解你的意思。如果您只想在将文件存储在“upload”目录中时重命名该文件,请在使用
move\u uploaded\u file()
时进行重命名:

您还可以通过在HTML表单中提供额外的“重命名”文本字段,让用户定义
$new\u filename


编辑:代码可以是这样的:

表格:


你不能给它另一个名字作为move_uploaded_file()中的第二个参数吗?
$destination = "upload/" . $new_filename;
if (file_exists($destination)) {
    echo 'File ', $destination, ' already exists!';
} else {
    move_uploaded_file($temp_filename, $destination);
}
<form action="upload_file.php" method="post"
    enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />

<!-- NEW TEXTBOX -->
<label for="newname">Rename to (optional):</label>
<input type="text" name="newname" id="newname" /> 
<br />

<input type="submit" name="submit" value="Submit" />
</form>
$upload_dir = realpath('upload') . DIRECTORY_SEPARATOR;
$file_info = $_FILES['file'];

// Check if the user requested to rename the uploaded file
if (!empty($_POST['newname'])) {
    $new_filename = $_POST['newname'];
} else {
    $new_filename = $file_info['name'];
}

// Make sure that the file name is valid.
if (strpos($new_filename, '/') !== false || strpos($new_filename, '\\') !== false) {
    // We *have* to make sure that the user cannot save the file outside
    // of $upload_dir, so we don't allow slashes.
    // ATTENTION: You should do more serious checks here!
    die("Invalid filename");
}

$destination = $upload_dir . $new_filename;
// ... if (file_exists(... move_uploaded_file(...
//form submit in database and file store in the documents folder 

$target_dir = "assets/documents/";

$target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);

$uploadOk = 1;

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {

} 

$images = basename($_FILES["imageUpload"]["name"],""); 
 public static function uploadFile($filepath="upload",$uniq=0){
   global $_FILES;
   try {
        // Undefined | Multiple Files | $_FILES Corruption Attack
        // If this request falls under any of them, treat it invalid.
        if (
            !isset($_FILES['uploaded_file']['error']) ||
            is_array($_FILES['uploaded_file']['error'])
        ) {
            $result["status"]="fail";$result["errors"]=('Invalid parameters.');return $result;
        }


        // Check $_FILES['uploaded_file']['error'] value.
        switch ($_FILES['uploaded_file']['error']) {
            case UPLOAD_ERR_OK:
                break;
            case UPLOAD_ERR_NO_FILE:
                $result["status"]="fail";$result["errors"]=('No file sent.');return $result;
            case UPLOAD_ERR_INI_SIZE:
            case UPLOAD_ERR_FORM_SIZE:
                $result["status"]="fail";$result["errors"]=('Exceeded filesize limit.');return $result;
            default:
                $result["status"]="fail";$result["errors"]=('Unknown errors.');return $result;
        }

        // You should also check filesize here. 
        if ($_FILES['uploaded_file']['size'] > 1000000) {
            $result["status"]="fail";$result["errors"]=('Exceeded filesize limit.');return $result;
        }

        // DO NOT TRUST $_FILES['uploaded_file']['mime'] VALUE !!
        // Check MIME Type by yourself.
        $finfo = new finfo(FILEINFO_MIME_TYPE);
        if (false === $ext = array_search(
            $finfo->file($_FILES['uploaded_file']['tmp_name']),
            array(
                'jpg' => 'image/jpeg',
                'png' => 'image/png',
                'gif' => 'image/gif',
            ),
            true
        )) {
            $result["status"]="fail";$result["errors"]=('Invalid file format.');return $result;
        }
        if($uniq==0){
            $temp=$filepath;
        }
        else{
            $temp=$filepath."/".uniqid()."_".$_FILES['uploaded_file']['name'];
        }
        if(@copy($_FILES['uploaded_file']['tmp_name'], $temp)) {
            return $result["status"]="success";
        } 
        $result["status"]="fail";$result["errors"]=('Unknown errors.');return $result;

    } catch (Exception $e) {

            $result["status"]="fail";$result["errors"]= $e->getMessage();return $result;

    }
}