你好我试图用php创建一个文件Uploader,所以我复制了它 这工作但后来我不想把它放在这样的函数中:
function upload($file) {
$target_dir = "www/";
$target_file = $target_dir . date("Y-m-d") . "_" . basename($file["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
$check = getimagesize($file["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 $target_file already exists.";
$uploadOk = 0;
}
// Check file size
if ($file["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($file["tmp_name"], $target_file)) {
echo "The file " . basename($file["name"]) . " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
return $target_file;
}
所以我可以这样称呼它:
include 'upload.php';
if(isset($_POST["send2"])) {
$p1="";
$p2="";
$p3="";
$p4="";
$p5="";
if(isset($_FILES["p1"])){
$p1= upload($_FILES["p1"]);
}
if(isset($_FILES["p2"])){
$p2= upload($_FILES["p2"]);
}
if(isset($_FILES["p3"])){
$p3=upload($_FILES["p3"]);
}
if(isset($_FILES["p4"])){
$p4 = upload($_FILES["p4"]);
}
if(isset($_FILES["p5"])){
$p5= upload($_FILES["p5"]);
}
$pcrud->create($_POST['titel'], $_POST['input'], "" . getdate() . "", $target_file, $_POST['p2'], $_POST['p3'], $_POST['p4'], $_POST['p5'], $_SESSION['user'], $ccrud->getidbytitle($_POST['cat']));
}
但是这个消息没有工作"文件名不能为空"来了。我怎样才能解决这个问题呢?
答案 0 :(得分:0)
我发现问题我无法检查文件是否已设置,我必须检查名称是否具有这样的值:
这就是我调用函数的方式:
if(isset($_POST["send2"])) {
$p1="";
$p2="";
$p3="";
$p4="";
$p5="";
if($_FILES["p1"]["name"]!=""){
$p1= upload($_FILES["p1"]["name"],$_FILES["p1"]["tmp_name"],$_FILES["p1"]["size"]);
}
if($_FILES["p2"]["name"]!=""){
$p2= upload($_FILES["p2"]["name"],$_FILES["p2"]["tmp_name"],$_FILES["p2"]["size"]);
}
if($_FILES["p3"]["name"]!=""){
$p3=upload($_FILES["p3"]["name"],$_FILES["p3"]["tmp_name"],$_FILES["p3"]["size"]);
}
if($_FILES["p4"]["name"]!=""){
$p4 = upload($_FILES["p4"]["name"],$_FILES["p4"]["tmp_name"],$_FILES["p4"]["size"]);
}
if($_FILES["p5"]["name"]!=""){
$p5= upload($_FILES["p5"]["name"],$_FILES["p5"]["tmp_name"],$_FILES["p5"]["size"]);
}
$pcrud->create($_POST['titel'], $_POST['input'], "" . getdate() . "", $p1, $p2, $p3, $p4, $p5, $_SESSION['user'], $ccrud->getidbytitle($_POST['cat']));
}
这就是功能:
function upload($filename,$filetmpname,$size) {
$target_dir = "www/";
$target_file = $target_dir . date("Y-m-d") . "_" . basename($filename);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
$check = getimagesize($filetmpname);
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 $target_file already exists.";
$uploadOk = 0;
}
// Check file size
if ($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($filetmpname, $target_file)) {
echo "The file " . basename($filename) . " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
return $target_file;
}