我試圖上傳圖像文件到我的本地文件夾(本地託管)。我設法做到這一點。問題是,我沒有在sql表中看到圖像路徑,包括假設在表中插入的其餘數據。之前,我能夠,之前我搞砸了。我以前對upload.php做了一些小修改。即使我設法將圖像文件上傳到本地文件夾(與我的網頁相同的目錄)。我從早上起就嘗試過,現在是午夜。我也得到一個錯誤 「未定義指數:image..line 106」 - >>圖像保存到文件夾,但路徑未保存在sql
$image = $_POST['image'];
請help.tq
下面是upload.php的。
<?php
//echo var_dump($_POST);
//echo var_dump($_FILES);
session_start();
require 'connect-test.php';
if(isset($_POST["submit"])) {
$id = $_POST['id'];
$name2 = $_POST['name2'];
$color2 = $_POST['color2'];
$hobby2 = $_POST['hobby2'];
$radiobtn = $_POST['radiobtn'];
$image = $_FILE['image'];
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image or not
$check = getimagesize($_FILES["image"]["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["image"]["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["image"]["tmp_name"], $target_file)) {
echo "The file ". basename($_FILES["image"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
$stmt = $conn->prepare("INSERT INTO useradvert (id,name2,color2,hobby2,radiobtn,image) VALUES (?,?,?,?,?,?)");
$stmt->bind_param("isssss",$id,$name2,$color2,$hobby2,$radiobtn,$target_file);
$stmt->execute();
}
?>
你已經張貼了類似的問題早http://stackoverflow.com/q/34567413 /關於同樣的問題。 –
@Fred。以前它是關於未定義的索引。現在,我設法將圖像插入文件夾,路徑除外。但在同一時間,我也收到一些錯誤。這是一個二合一的問題。 –
之前解決了一個問題後,會出現一個新問題,但會出現另一個問題。這有點複雜 –