jpg/jpeg 画像をアップロードする次の PHP コードは、アップロード エラー [つまり、「申し訳ありませんが、ファイルのアップロード中に問題が発生しました。定義による。]
印刷エラー コードは 0 を返しますが、データベース、アップロード ディレクトリ、サムネイル ディレクトリを確認すると、ファイルが追加されていないことがわかります。ディレクトリのアクセス許可を再確認しましたが、すべて問題ないようです。
ここで、この問題を解決する方法を提案できますか?
<?php
error_reporting(0);
if(isset($_POST['submit']))
{
$target = "../uploads/";
$allowedExts = array("jpg", "jpeg");
$extension = end(explode(".", $_FILES["file_upload"]["name"]));
$target = $target . basename( $_FILES['file_upload']['name']);
$date = date("Y-m-d H:i:s");
//Function to generate image thumbnails
function make_thumb($src, $dest, $desired_width) {
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination with 100% quality*/
imagejpeg($virtual_image, $dest,100);
}
//create a mysql connection
$dbhost = "localhost";
$dbuser = "xxxx";
$dbpass = "xxxx";
$dbname = "gallery";
// Create connection
$conn=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//check for allowed extensions
if ((($_FILES["file_upload"]["type"] == "image/jpg")|| ($_FILES["file_upload"]["type"] == "image/jpeg"))&& in_array($extension, $allowedExts))
{
$photoname = $_FILES["file_upload"]["name"];
if (file_exists("../uploads/" . $photoname))
{
die( '<div class="error">Sorry <b>'. $photoname . '</b> already exists</div>');
}
if(move_uploaded_file($_FILES['file_upload']['tmp_name'], $target))
{
$sqlquery = "INSERT INTO photos (photo_name,date_added) VALUES ('$photoname','$date')";
$query = mysqli_query($conn, $sqlquery);
$sql = "SELECT MAX(id) FROM photos";
$max = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($max);
$maxId = $row['MAX(id)'];
$type = $_FILES["file_upload"]["type"];
switch($type)
{
case "image/jpeg":
$ext = ".jpeg";
break;
case "image/jpg";
$ext = ".jpg";
break;
}
//define arguments for the make_thumb function
$source = "../uploads/".$photoname;
$destination = "../thumbnails/thumb_". $maxId . $ext ."";
//specify your desired width for your thumbnails
$width = "282";
//Finally call the make_thumb function
make_thumb($source,$destination,$width);
$msg = '<div class="success">
<b>Upload: </b>' . basename($photoname) . '<br />
<b>Type: </b>' . $_FILES["file_upload"]["type"] . '<br />
<b>Size: </b>' . ceil(($_FILES["file_upload"]["size"] / 1024)) . 'Kb<br />
</div>';
}
else
{
//$msg = $_FILES['file_upload']['error'];
$msg = '<div class="error">Sorry, there was a problem uploading your file.</div>';
}
}
else
{
$msg = '<div class="error">The file type you are trying to upload is not allowed!</div>';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload Image</title>
<link href="../css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<H3>Upload Images Here</H3>(<a href="../" target="_blank">View Gallery</a>)
<div id="upload">
<?php echo $msg; ?>
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file_upload" id="upload_file" />
<input type="submit" name="submit" value="Upload" />
</form>
</div>
</body>
</html>