我试图发送电子邮件"在发送电子邮件之前将文件上载到文件夹的表单。然后,文件的路径显示在电子邮件中,但如果文件存在,我不希望它们被覆盖,但我希望添加一个数字。
我编写了这个很小的代码,但它不起作用!
<击> 错误说:
<击>
解析错误:语法错误,意外&#39; $ name_of_uploaded_file&#39; (T_VARIABLE)位于第49行的/comersomers/8/5/6/HOMEPAGE/httpd.www/php/mailtest2.php
击>
如果文件不存在,下面的脚本现在上传文件。如果文件确实存在则没有任何反应。脚本应以此结束。
该脚本已被修改为导致它不是广告的原因&#34; _1&#34;到文件名并将文件保存在文件夹中。现在它将文件保存在文件夹中并出现错误:复制上传文件时出错
有人可以向我解释我的错误吗?
现在显示所有代码都要对此进行调试:
<?php
//Uploaded file 1
//Settings
$max_allowed_file_size = 2000; // size in KB
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp", "pdf");
error_reporting(E_ALL);
//Upload folder
//name also used later on mail
$name = $_POST['name'];
$d = date('d.m.y');
$varfoldername = "../receivedfiles/$name$d/";
if(!is_dir($varfoldername)) {
mkdir($varfoldername , 0777 , true);
}
$upload_folder = $varfoldername;
//Get the uploaded file information
$name_of_uploaded_file =
basename($_FILES['uploaded_file']['name']);
//Validations
//----- Check if files exists and adds _X to it -----
$original_name_of_file = pathinfo($name_of_uploaded_file, PATHINFO_FILENAME);
$extension = pathinfo($name_of_uploaded_file, PATHINFO_EXTENSION);
$FileCounter = 0;
while (file_exists($varfoldername.$name_of_uploaded_file)) {
$FileCounter++;
$name_of_uploaded_file = $original_name_of_file . '_' . $FileCounter . '.' . $extension;
}
//copy the temp. uploaded file to uploads folder
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
$tmp_path = $_FILES["uploaded_file"]["tmp_name"];
if(is_uploaded_file($tmp_path))
{
if(move_uploaded_file ( $tmp_path,$path_of_uploaded_file ))
{
die("Error while copying the uploaded file");
}
}
//Validate size requirements
$size_of_uploaded_file =
$_FILES["uploaded_file"]["size"]/1024;//size in KBs
if($size_of_uploaded_file > $max_allowed_file_size )
{
die("Fejl: Filen er for stor");
}
//------ Validate the file extension -----
//get the file extension of the file
$type_of_uploaded_file =
substr($name_of_uploaded_file,
strrpos($name_of_uploaded_file, '.') + 1);
$allowed_ext = false;
for($i=0; $i<sizeof($allowed_extensions); $i++)
{
if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
{
$allowed_ext = true;
}
}
if(!$allowed_ext)
{
die("The uploaded file is not supported file type. \n Send venligst filer af følgende type: .implode(',',$allowed_extensions)");
}
$email = $_POST['email'];
$phone = $_POST['phone'];
$call = $_POST['call'];
$company = $_POST['company'];
$type = $_POST['type'];
$adress = $_POST['adress'];
$hesteid = $_POST['hesteid'];
$hestenavn = $_POST['hestenavn'];
$message = $_POST['message'];
$areacode = $_POST['areacode'];
$land = $_POST['land'];
$formcontent=" Fra: $company \n Navn: $name \n Adresse: $adress , $areacode \n Land: $land \n Telefon: $phone \n Ringes op: $call \n Type: $type \n Hoppens navn og ID: $hestenavn , $hesteid \n Besked: \n $message \n Vedhæftede filer: \n $path_of_uploaded_file";
$recipient = "simon@secret.dk";
$subject = "Besked fra hjemmesiden";
$mailheader = "Fra: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: thank_you.shtml');
?>
答案 0 :(得分:1)
您需要使用.
字符连接(粘在一起)不同的PHP变量,所以:
while (file_exists($varfoldername$name_of_uploaded_file ))
会返回一个解析错误,因为你需要告诉PHP你要将两个变量串在一起,所以改为使用.
并写:
while (file_exists($varfoldername.$name_of_uploaded_file))
您可能还需要在两个变量之间添加/
目录分隔符,但这是一个与当前问题无直接关系的问题。但它对print $varfoldername.$name_of_uploaded_file
很有用,看看它是否是正确的文件路径布局(包含所有/
等)
嗨,我再次修改了这个问题。发现了两个愚蠢的错误。现在脚本运行没有错误,但不会在文件名中添加“_1”。 - 西蒙·詹森
重新排列代码:
$FileCounter = 0;
$original_name_of_file = $name_of_uploaded_file;
while (file_exists($varfoldername.$name_of_uploaded_file)) {
$FileCounter++;
$name_of_uploaded_file = $original_name_of_file . '_' . $FileCounter . '.' . $extension;
}
这里发生了什么:
while循环中的PHP会覆盖$name_of_uploaded_file
,这样如果你有一个file_1.txt
,那么循环中的下一个文件名将是file_1.txt_2.txt
,你可以看到这个覆盖远非完美,而是需要保存原始文件,然后用增量值和.txt覆盖连接的原始文件。
未定义$name_of_uploaded_file
的原始值。
$FileCounter
在写入字符串之前会增加。
$FileCounter++
的代码无法使用,因为您已将$FileCounter
定义为 string
,方法是在引号中设置它。我删除了引号,因此它现在被PHP识别为整数。
您的file_exists
来电 应该在引号中,因为这会导致PHP额外和不必要的工作,并且通常会让您感到困惑以及你的IDE。有效的报价是:
PHP Logic:我发现了一个引用,我将启动一个字符串结构,哦,这个 部分看起来像一个变量,我将停止字符串结构,然后 然后将此变量值连接到字符串结构中 继续字符串哦,另一个变量,我会停止字符串 结构,然后将第二个变量连接到此字符串中, 然后继续。我找到了另一个引号,所以字符串完成了。
与明确简洁地定义与.
连接的两个变量相比,这是一项更大的工作量。
$name = $_POST['name'];
$d = date('d.m.y');
/***
Ideally folders should NEVER be relative, always base them from the PHP server
root such as (example only), using $_SERVER['DOCUMENT_ROOT']:
$varfoldername = $_SERVER['DOCUMENT_ROOT']."/data/uploads/receivedfiles/".$name.$d."/";
***/
$varfoldername = "../receivedfiles/".$name.$d."/";
$upload_folder = $varfoldername;
$name_of_uploaded_file = basename($_FILES['uploaded_file']['name']);
$original_name_of_file = pathinfo($name_of_uploaded_file, PATHINFO_FILENAME);
$extension = pathinfo($name_of_uploaded_file, PATHINFO_EXTENSION);
$FileCounter = 0;
while (file_exists($varfoldername.$name_of_uploaded_file)) {
$FileCounter++;
$name_of_uploaded_file = $original_name_of_file . '_' . $FileCounter . '.' . $extension;
}
/***
Now $name_of_uploaded_file will have the numerical appendage as needed.
***/
if(move_uploaded_file( $tmp_path , $varfoldername.$name_of_uploaded_file )){
print "file uploaded!";
}
重要强>
请勿使用copy
,而应使用PHP Move_upload_file:
if(!copy($tmp_path,$path_of_uploaded_file)){ ...
变为
if(move_uploaded_file ( $tmp_path , $path_of_uploaded_file )){ ...
答案 1 :(得分:1)
当你可以用它来制作一个可重复使用的对象时,没有太多需要使用这个长方法。
interface FileUploaderInterface
{
public function upload($path);
}
class FileUploader implements FileUploaderInterface
{
protected $File;
public function __construct($file)
{
$this->File = $file;
return $this;
}
public function upload($path)
{
if(file_exists($path.$this->File['name']))
{
$this->File["tmp_name"] = $this->File["name"] . '-' . time();
move_uploaded_file($this->File, $path . $this->File["name"]);
return false;
}
else
{
move_uploaded_file($this->File, $path . $this->File["name"]);
return true;
}
}
}
只需使用require_once()
然后使用此代码即可使用:
$obj = new FileUploader($_FILES["fileToUpload"]);
if(!$obj->upload(dirname(__FILE__) . '/example/path/ect')) {
echo "File existed, we added the time onto the name";
}
答案 2 :(得分:0)
while (file_exists($varfoldername . $name_of_uploaded_file ))
也许你需要$varfoldername . '/' . $name_of_uploaded_file
- 如果你能给我们这两个变量的 var_dump ,那就最好了。