2017-12-10 21 views
0

こんにちは、 私は非常に単純なものを達成しようとしています。 私は、新しいものを送ることによって変更できるイメージを表示するページを取得したい、それは私の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を持つ画像を1つだけストックしたいと思います(理由を知りたいのであれば、それはまっすぐに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

関連する問題