<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
任何回复都表示赞赏。谢谢:))
答案 0 :(得分:0)
PHP move_uploaded_file(file, newloc)
有第二个选项作为源目标+文件名。像:
move_uploaded_file(file, '/var/www/html/project/public'.$filename);
此处$filename
是将其移动到您的服务器的文件名。您可以为文件指定任何名称,如时间戳值或用户指定的字符串。
在你的情况下:
$filename = $_REQUEST['filename'];
答案 1 :(得分:0)
目标文件名真正重要的是以下几行:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
所以,您只需将POST请求发送到PHP,就像您可能已经完成的那样:
<form method="POST" action="target_process.php">
<input type="text" name="filename">
...(other form objects here)
</form>
并在PHP中检索信息,并将其附加到target_file。另外,将扩展名类型附加到文件的末尾。
<?php
$target_dir = "uploads/";
$file_parts = explode('.', $_FILES["fileToUpload"]["name"]);
$ext = $file_parts[count($file_parts) - 1];
$target_file = $target_dir . $_POST['filename'] . '.' . $ext;