.php 如何显示我可以更改并存储在我的目录中的图像
.php how to display an image that I can change and stock on my directory
你好,
我试图在 php 中实现一些非常简单的事情。
我想要一个显示图像的页面,我可以通过发送新图像来更改该图像,并且该图像与我的 php 存放在同一文件夹中。因此,当我打开我的页面时,我将上传最后一张图片。
理想情况下,我可以像 croppie 那样裁剪它 (https://foliotek.github.io/Croppie/),但我想了解这些步骤,所以让我们保持简单,裁剪将在以后使用。
到目前为止,允许我上传图片的代码是
upload.php:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["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["fileToUpload"]["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["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
和index.html
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
所以我不仅想预览显示的图像,而且我还想只储存一张具有特定 ID 的图像(如果您想知道为什么,那是因为它会直接进入 indesign,无论如何。 ..)
感谢大家的帮助!!!
首先在index.html中创建以下代码
<?php
$path = 'i/';
$tmp_path = 'tmp/';
$types = array('image/gif', 'image/png', 'image/jpeg');
$size = 1024000;
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!in_array($_FILES['picture']['type'], $types))
die('wrong type. <a href="?">maybe another file?</a>');
// Check size
if ($_FILES['picture']['size'] > $size)
die('BIG. <a href="?">maybe another file?</a>');
// Upload
if (!@copy($_FILES['picture']['tmp_name'], $path . $_FILES['picture']['name']))
echo 'wrong';
else
echo 'good <a href="' . $path . $_FILES['picture']['name'] . '">see</a> ' ;
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="picture">
<input type="submit" value="Upload">
</form>
</body>
</html>
这是一坨屎代码。尝试分享来自 html
的 php 代码
http://php.net/manual/en/features.file-upload.post-method.php
我找到的解决方案
灵感来自该页面
How to upload & Save Files with Desired name
<form action='' method='POST' enctype='multipart/form-data'>
<input type='file' name='userFile'><br>
<input type='submit' name='upload_btn' value='upload'>
</form>
<?php
$info = pathinfo($_FILES['userFile']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = "newname.".$ext;
$target = 'images/'.$newname;
move_uploaded_file( $_FILES['userFile']['tmp_name'], $target);
?>
<img src="images/newname.png">
干净高效!效果不错!
你好, 我试图在 php 中实现一些非常简单的事情。 我想要一个显示图像的页面,我可以通过发送新图像来更改该图像,并且该图像与我的 php 存放在同一文件夹中。因此,当我打开我的页面时,我将上传最后一张图片。 理想情况下,我可以像 croppie 那样裁剪它 (https://foliotek.github.io/Croppie/),但我想了解这些步骤,所以让我们保持简单,裁剪将在以后使用。
到目前为止,允许我上传图片的代码是
upload.php:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["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["fileToUpload"]["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["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
和index.html
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
所以我不仅想预览显示的图像,而且我还想只储存一张具有特定 ID 的图像(如果您想知道为什么,那是因为它会直接进入 indesign,无论如何。 ..)
感谢大家的帮助!!!
首先在index.html中创建以下代码
<?php
$path = 'i/';
$tmp_path = 'tmp/';
$types = array('image/gif', 'image/png', 'image/jpeg');
$size = 1024000;
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!in_array($_FILES['picture']['type'], $types))
die('wrong type. <a href="?">maybe another file?</a>');
// Check size
if ($_FILES['picture']['size'] > $size)
die('BIG. <a href="?">maybe another file?</a>');
// Upload
if (!@copy($_FILES['picture']['tmp_name'], $path . $_FILES['picture']['name']))
echo 'wrong';
else
echo 'good <a href="' . $path . $_FILES['picture']['name'] . '">see</a> ' ;
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="picture">
<input type="submit" value="Upload">
</form>
</body>
</html>
这是一坨屎代码。尝试分享来自 html
的 php 代码http://php.net/manual/en/features.file-upload.post-method.php
我找到的解决方案 灵感来自该页面 How to upload & Save Files with Desired name
<form action='' method='POST' enctype='multipart/form-data'>
<input type='file' name='userFile'><br>
<input type='submit' name='upload_btn' value='upload'>
</form>
<?php
$info = pathinfo($_FILES['userFile']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = "newname.".$ext;
$target = 'images/'.$newname;
move_uploaded_file( $_FILES['userFile']['tmp_name'], $target);
?>
<img src="images/newname.png">
干净高效!效果不错!