2017-12-10 52 views
0

안녕하세요, 저는 PHP에서 아주 간단한 것을 달성하려고합니다. 새 이미지를 보내어 변경할 수있는 이미지를 표시하는 페이지를 가져오고 싶습니다.이 페이지는 내 PHP와 동일한 폴더에 저장되어 있습니다. 따라서 내 페이지를 열면 마지막으로 업로드 한 이미지를 갖게됩니다. Idealy 나는 croppie (https://foliotek.github.io/Croppie/)와 같은 방법으로 작물을 재배 할 수있을 것입니다. 그러나 단계를 이해하고 간단하게 유지하면 곡물은 나중에 저장됩니다..php 내 디렉토리에서 변경할 수있는 이미지를 표시하는 방법

나는 나 이미지를 업로드 할 수 있도록 지금까지 한 코드는

upload.php로 :

<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> 

그래서뿐만 아니라 내가 갖고 싶어

<?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을 이미지의 미리보기가 표시되지만 특정 ID가있는 이미지 하나만 재고하려고합니다 (이유를 알고 싶다면 indesign으로 곧바로 이동하기 때문에 ...)

도움 주셔서 감사합니다. !!!

답변

1

솔루션을 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"> 

깨끗하고 효율적인에서 영감을 을 발견했습니다! 잘 작동합니다!

0

먼저 코드

<?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 ([email protected]($_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> 

그것은 똥 코드의 다음 index.html을에서 만들 수 있습니다. HTML

에서

http://php.net/manual/en/features.file-upload.post-method.php

+0

이 코드를 시도하면 "잘못된"에코가 나타납니다 ... – Odjone