-1
你好,我試圖與PHP文件上傳,所以我拷入它 這個工作,但後來我wan't把這個在這樣的函數:文件上傳功能
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']));
}
但這不起作用的消息「文件名不能爲空」來了。我該如何解決這個問題有什麼問題?
「沒有工作」是一個廣泛的問題。究竟會發生什麼? –
您嘗試上傳的文件夾是否具有寫入權限? –
是的,它有。它工作時,我使用$ _Files ['nameoffile'] –