如何保存PHP图像?

时间:2016-12-14 01:04:35

标签: php html html5

我如何以及在何处存储用户照片?我在这里阅读了一些非常好的答案,但他们没有解释我要写的代码和发送照片的位置。

到目前为止,这是我的代码:

profile.php:

<form id="form2" action="upload.php" method="post" enctype="multipart/form-data">
<p id="p1">Change profile picture:</p> <br />
<input type="file" name="fileToUpload" id="fileToUpload"><br />
<br><input id="sub1" type="submit" value="Change profile picture" name="submit"><br />
</form>

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 if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}   
$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;
}
$save_path="userimg"; // Folder where you wanna move the file.
$myname = strtolower($_FILES['image']['tmp_name']); //You are renaming the file here
move_uploaded_file($_FILES['image']['tmp_name'], $save_path.$myname); // Move the uploaded file to the desired folder
}
?>

我在这里读到像Facebook推特等大型网站不会在数据库上保存文件,因为它们占用了很多服务器空间,所以将它们保存在文件夹中。有人可以帮助我逐步完成这项工作吗?

2 个答案:

答案 0 :(得分:0)

假设您的代码没有错误,请让我解释一下这里发生了什么。 HTML部分非常清楚,multipart事情用来对浏览器说,我们将上传文件。

所以,跳转到PHP代码:

<?php

$target_dir = "uploads/"; 
//This is the directry where the images will be uploaded, "server_path/uploads"

//Now since we have the target directory, let's define the actual path.
//Actual Path = Target Dir + File Name + File Extension

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

//By now, we have just madefined locations, (in the form of Variables)

//Now, lets do few checks, and make a success flag to keep a track
$uploadOk = 1;



// Check if file already exists
if (file_exists($target_file)) {
    //If the file already exists
    echo "Sorry, file already exists.";
    $uploadOk = 0;
    //Echo and change the flag to False
}  


// Check if image file is a actual image or fake image.
//If file is image, then only image size will be returned
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if(!($check)) {
    //That is, the Check is false, the file is NOT AN IMAGE
    echo "File is not an image.";
    $uploadOk = 0;
    //Echo, and change the Flag to false.
}


//Now check, if the flag is true, that means upload is OK, and NO ERRORS.
//Then upload the file.
if ($uploadOk) {
    //If upload is OK, then go ahead
    move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file);
    // Move the uploaded file to the desired folder
}
?>

这只是上传文件,可以实施其他检查,例如检查文件大小等。More Info on this W3 Page

正在做什么?  首先,只要在“选择文件”按钮上选择文件,该文件就会引用只有计算机知道的fakepath(数据抽象)。然后在提交文件时,图像数据从浏览器(fakepath)发送到服务器,服务器存储在temp_dir(或等效文件夹,对于临时存储),然后开始执行脚本。在脚本中,因为我们已经提到了存储$target_dir的位置和文件名,然后是move_uploaded_file,所以服务器知道,现在是时候从{ {1}}到一个真实路径......然后你得到该目录中的文件。如果fakepath没有出现,那么临时文件在执行脚本后会被取消链接,或者在此之后的某个时间(取决于您如何配置服务器,move_uploaded_file文件)。

注意:上传目录(在这种情况下为php.ini),需要已经创建,服务器只会添加文件,如果floder已经不在那里,它将执行,(没有任何错误),但文件不会显示,(因为没有文件夹可以保存它)。

现在,您想要将其添加到数据库中,您可以保留此链接并将其添加到数据库(实际上,uploads/数据库),但这是另一回事。 (我刚才说,因为你在问题中提到了它。)

注意:您可能需要根据需要更改代码以使用它。

希望这会有所帮助:)

答案 1 :(得分:0)

$image14 = $_FILES['img']['name'];                     //img your form input name="img"
$image15 = $_FILES['img']['tmp_name'];
$ext     = pathinfo($image14, PATHINFO_EXTENSION);
$path2   = "images/" . time() . $image14;              // "image/" specify your foldername
move_uploaded_file($image15, $path2);                  // save $path2  into your database