使用此代码一段时间后,我发现人们正在上传带有空格和逗号的文件。
我已经尝试了函数reg_replace
,并且上传的文件名实际上已编辑,其中的空格被替换为所谓的下划线。
我还需要告诉用户最终的文件名,因此如果上传成功,我需要在文本字段中包含该文件名。最后一部分是缺失的部分。
如何在以下背景下完成?
<?php
$target_dir = "extra_images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>File already exists.</strong></div>";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 3750000) {
echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>Your file is too large.</strong></div>";
$uploadOk = 0;
}
//Check for pdf format
if (!empty($_FILES['fileToUpload']['tmp_name'])) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES['fileToUpload']['tmp_name']);
if (($mime != 'application/pdf') && ($mime != 'image/jpg') && ($mime != 'image/jpeg') && ($mime != 'image/gif') && ($mime != 'image/png')) {
$uploadOk = 0;
echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>This file is not a valid file.</strong></div>";
//exit();
}} //this bracket was missing I think
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>The file was not uploaded.</strong></div>";
// if everything is ok, try to upload file
} else {
$target_file = preg_replace('/\s+/', '_', $target_file);//to replace spaces...
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "<div class=\"alert alert-success\" role=\"alert\">The file <strong>". basename( $_FILES["fileToUpload"]["name"]). "</strong> has been uploaded.</div><br>Please copy this filename: <span class=\"form-inline\"><input type=\"text\" value=\"". basename( $_FILES["fileToUpload"]["name"]). "\" class=\"form-control input-sm\" style=\"width:220px;\" /></span> And paste it in an empty Extra image field above and save the form.";
} else {
echo "<div class=\"alert alert-danger\" role=\"alert\">There was an error uploading your file.</div>";
}
}
echo "</br></br><p><button class=\"btn btn-default pull-right\" style=\"margin-right:5px;\" type=\"submit\" onclick=\"javascript:history.go(-1)\"><span class=\"glyphicon glyphicon-step-backward\" aria-hidden=\"true\"></span> Back</button></p>";
exit();
?>
答案 0 :(得分:1)
您可以使用str_replace()将空格更改为下划线。
$str = 'hai welcome';
$newstr = str_replace(' ', '_', $str);
echo $newstr;
现在你的输出没有空间;
答案 1 :(得分:0)
好吧,我设法让它工作,它现在将文件名保存到数据库,这是我的其他需求之一。用户ip捕获也可以,但只能使用$ _SERVER ['REMOTE_ADDR'];变量我想知道如何使用get_client_ip()。试图将$ ipaddress保存到db字段但没有工作...感谢您的想法和帮助。
<?php
$target_dir = "images_folder/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$target_file = preg_replace('/\s+/', '_', $target_file);//to replace spaces...
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Function to get the client IP address
function get_client_ip() {
$ipaddress = '';
if ($_SERVER['HTTP_CLIENT_IP'])
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if($_SERVER['HTTP_X_FORWARDED_FOR'])
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if($_SERVER['HTTP_X_FORWARDED'])
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if($_SERVER['HTTP_FORWARDED_FOR'])
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if($_SERVER['HTTP_FORWARDED'])
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if($_SERVER['REMOTE_ADDR'])
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
$ipaddress2=$_SERVER['REMOTE_ADDR'];
// Check if file already exists
if (file_exists($target_file)) {
echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>File already exists.</strong></div>";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 3750000) {
echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>Your file is too large.</strong></div>";
$uploadOk = 0;
}
//Check for pdf format
if (!empty($_FILES['fileToUpload']['tmp_name'])) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES['fileToUpload']['tmp_name']);
if (($mime != 'application/pdf') && ($mime != 'image/jpg') && ($mime != 'image/jpeg') && ($mime != 'image/gif') && ($mime != 'image/png')) {
$uploadOk = 0;
echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>This file is not a valid file.</strong></div>";
//exit();
}} //this bracket was missing I think
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>The file was not uploaded.</strong></div>";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$cleanFilename=basename("$target_file");
// Connects to your Database
mysql_connect("localhost", "db_username", "db_pass") or die(mysql_error()) ;
mysql_select_db("db_name") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO tableName (filename,ipaddress)
VALUES ('$cleanFilename', '$ipaddress2')") ;
echo "<div class=\"alert alert-success\" role=\"alert\">The file <strong>". basename( $_FILES["fileToUpload"]["name"]). "</strong> has been uploaded.</div><br>Please copy this filename: <span class=\"form-inline\"><input type=\"text\" value=\" $cleanFilename \" class=\"form-control input-sm\" style=\"width:320px;\" /></span> And paste it in an empty Extra image field above and save the form. If you see the image after saving you've done right. $cleanFilename";
} else {
echo "<div class=\"alert alert-danger\" role=\"alert\">There was an error uploading your file.</div>";
}
}
echo "</br></br><p><button class=\"btn btn-default pull-right\" style=\"margin-right:5px;\" type=\"submit\" onclick=\"javascript:history.go(-1)\"><span class=\"glyphicon glyphicon-step-backward\" aria-hidden=\"true\"></span> Back</button></p>";
exit();
?>