2017-06-27 11 views
0

私は本当にhtmlやPHPに新しいです。私はサーバーに画像をアップロードするために使用される、フォームの作業をしようとしています。フォームを介して画像を送信した後、成功またはエラーメッセージを表示しますか?

<form class="myBtn" id="Upload" action="upload.php" method="post" enctype="multipart/form-data" style="visibility:hidden;"><br> 
     Select template to upload:<br> 
     <input type="file" name="fileToUpload" id="fileToUpload"><br> 
     <input type="submit" value="Upload Image" name="submit"> 
</form> 

これは私のHTML形式です。ここで

<?php $target_dir = "uploads/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
$needheight = 355; 
$needwidth = 275; 

// 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 "Checked: File is an image! - " . $check["mime"] . "."; 
     $uploadOk = 1; 
    } else { 
     echo "Checked: File is not an image!"; 
     $uploadOk = 0; 
    } 
} 

if (file_exists($target_file)) { 
    echo "Sorry, file already exists."; 
    $uploadOk = 0; 
} 
if ($_FILES["fileToUpload"]["size"] > 1048576) { 
    echo "Sorry, your file is too large."; 
    $uploadOk = 0; 
} 
if($imageFileType != "png") { 
    echo "Sorry, only .PNG is allowed."; 
    $uploadOk = 0; 
} 

if ($uploadOk == 0) { 
    echo "Sorry, your file was not uploaded."; 
} 

else { 
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
     echo "Your cape template". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
    } else { 
     echo "Sorry, there was an error uploading your template. Try again."; 
    } 
} 

?> 

あなたは私のPHPコードを見ることができます。私は本当にそのようなWoC(コードの壁)が、あなたが私を助けるために必要なコードの特定の部分を本当に知っていないことは本当に残念です。

私は年を取ったPHPの作業を得ましたが、私はそれを行いました。私は正常に私のサーバーにテンプレートをアップロードすることができます。 しかし、テンプレートを正常に送信した後、別の空白のページにリダイレクトされます: "Checked:ファイルは画像です - image/png!あなたのケープテンプレートがアップロードされました!"

空白のページにリダイレクトしないで、メインのウェブサイトで成功またはエラーのエコーを表示する方法はありますか?

ありがとう、私は瞬間に立ち往生しています。

答えて

0

フォームからアクションを削除して、ページを同じページに投稿します。そして、あなたの HTML befoe <form>upload.php

<?php $target_dir = "uploads/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
$needheight = 355; 
$needwidth = 275; 

// 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 "Checked: File is an image! - " . $check["mime"] . "."; 
     $uploadOk = 1; 
    } else { 
     echo "Checked: File is not an image!"; 
     $uploadOk = 0; 
    } 
} 

if (file_exists($target_file)) { 
    echo "Sorry, file already exists."; 
    $uploadOk = 0; 
} 
if ($_FILES["fileToUpload"]["size"] > 1048576) { 
    echo "Sorry, your file is too large."; 
    $uploadOk = 0; 
} 
if($imageFileType != "png") { 
    echo "Sorry, only .PNG is allowed."; 
    $uploadOk = 0; 
} 

if ($uploadOk == 0) { 
    echo "Sorry, your file was not uploaded."; 
} 

else { 
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
     echo "Your cape template". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
    } else { 
     echo "Sorry, there was an error uploading your template. Try again."; 
    } 
} 

?> 

<form class="myBtn" id="Upload" action="" method="post" enctype="multipart/form-data" style="visibility:hidden;"><br> 
     Select template to upload:<br> 
     <input type="file" name="fileToUpload" id="fileToUpload"><br> 
     <input type="submit" value="Upload Image" name="submit"> 
</form> 
+0

私はPHPコードを自分のHTMLのフォームのすぐ上に移動して、アクション= "を削除しました。完全に白いテキストで書かれています。これは正常ですか? (昇華を使用して)。 画像をアップロードしようとすると、ウェブページが更新されます(これ以上のリダイレクトはありません)。アップロードはされません。 –

+0

ファイル拡張子が '.html'ではなく' .php'であることを願っています –

+0

特定のファイル拡張子は何ですか?私はhtmlとphpの完全な初心者ですか? –

0

あなたは、おそらくこのような何かを行うことができ、現在でPHPからコードを書く - 1つのページにすべてのコードを維持し、フォームが送信された後に、関連するメッセージが表示されます。

<?php 


    $errors=array(); 
    $allowed=array('png'); 
    $message = false; 

    /* A utility function to report actual error (if any) */ 
    function uploaderror($code){ 
     switch($code) { 
      case UPLOAD_ERR_INI_SIZE: return "The uploaded file exceeds the upload_max_filesize directive in php.ini"; 
      case UPLOAD_ERR_FORM_SIZE: return "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"; 
      case UPLOAD_ERR_PARTIAL: return "The uploaded file was only partially uploaded"; 
      case UPLOAD_ERR_NO_FILE: return "No file was uploaded"; 
      case UPLOAD_ERR_NO_TMP_DIR: return "Missing a temporary folder"; 
      case UPLOAD_ERR_CANT_WRITE: return "Failed to write file to disk"; 
      case UPLOAD_ERR_EXTENSION: return "File upload stopped by extension"; 
      default: return "Unknown upload error"; 
     } 
    } 


    /* Only process this if these condition are met */ 
    if(isset($_POST['submit'], $_FILES['fileToUpload'])) { 

     /* Obtain reference to upload - Object notation is quicker/easier imo */ 
     $obj=(object)$_FILES['fileToUpload']; 
     $name=$obj->name; 
     $tmp=$obj->tmp_name; 
     $size=$obj->size; 
     $type=$obj->type; 
     $error=$obj->error; 

     /* configuration */ 
     $target_dir = $_SERVER['DOCUMENT_ROOT']."/uploads/"; 
     $target_file = $target_dir . $name; 
     $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION)); 
     $needheight = 355; 
     $needwidth = 275; 




     if($error == UPLOAD_ERR_OK && is_uploaded_file($tmp)){ 
      /* Is it an image */ 
      $check = getimagesize($tmp); 
      if(!$check)$errors[]='Checked: File is not an image!'; 
      else{ 
       /* Find dimensions and other attributes */ 
       list($width, $height, $type, $attr) = $check; 
       if($width < $needwidth or $height < $needheight)$errors[]='Image is too small'; 
      } 

      if(file_exists($target_file))$errors[]='Sorry, that file already exists.'; 
      if($size > 1048576) $errors[]='Sorry, your file is too large.'; 
      if(!in_array($ext, $allowed)) $errors[]='Sorry, only images of type .'.strtoupper(implode(', .',$allowed)).' '.(count($allowed)==1 ? 'is' : 'are').' allowed.'; 


      clearstatcache(); 


      if(empty($errors)){ 
       $status = move_uploaded_file($tmp, $target_file); 
       $message = $status ? "Your cape template ". basename($name). " has been uploaded." : "Sorry, there was an error uploading your template. Try again."; 
      }   

     } else { 
      exit(uploaderror($error)); 
     } 
    } 
?> 
<!doctype html> 
<html> 
    <head> 
     <title>File upload</title> 
    </head> 
    <body> 
     <form class='myBtn' method='post' enctype='multipart/form-data'> 
      Select template to upload:<br> 
      <input type="file" name="fileToUpload" id="fileToUpload"><br> 
      <input type="submit" value="Upload Image" name="submit"> 
     </form> 
     <?php 
      if($_SERVER['REQUEST_METHOD']=='POST' && !empty($message)){ 
       echo $message; 
      } 
      if(!empty($errors)){ 
       foreach($errors as $error) echo $error . '<br />'; 
      } 
     ?> 
    </body> 
</html> 
+0

の直前にPHPのコードを追加するか、<!doctype html>を上回らなければなりませんか? –

+0

Doctype宣言の前にphpを付けて、上記のコードをそのまま実行できるはずです – RamRaider

0

嬉しいことに、urselfさんがPHPの部分を完成しました。 HTMLとPHPの2つのファイルを1つのファイルとして使用し、$ _SERVER ['PHP_SELF']を使用しています。

<form class="myBtn" id="Upload" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"><br> 
<label>Select template to upload:</label><br> 
    <input type="file" name="fileToUpload" id="fileToUpload"><br> 
    <input type="submit" value="Upload Image" name="submit"> 
</form> 

<?php 
$message = ""; 
// Check if image file is a actual image or fake image 
if(isset($_POST["submit"])) { 
$target_dir = "/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
$needheight = 355; 
$needwidth = 275; 
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
if($check !== false) { 
    echo $message = "Checked: File is an image! - " . $check["mime"] . "."; 
    $uploadOk = 1; 
} else { 
    echo $message = "Checked: File is not an image!"; 
    $uploadOk = 0; 
} 
if (file_exists($target_file)) { 
echo $message ="Sorry, file already exists."; 
$uploadOk = 0; 
} 
if ($_FILES["fileToUpload"]["size"] > 1048576) { 
echo $message ="Sorry, your file is too large."; 
$uploadOk = 0; 
} 
if($imageFileType != "png") { 
echo $message ="Sorry, only .PNG is allowed."; 
$uploadOk = 0; 
} 

if ($uploadOk == 0) { 
echo $message ="Sorry, your file was not uploaded."; 
} 

else { 
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
echo $message ="Your cape template". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
} else { 
echo $message ="Sorry, there was an error uploading your template. Try again."; 
} 
} 
} 
?> 

<script> 
var alert_msg = "<?php echo $message; ?>"; 
<?php if(strlen($message) > 1) { ?> 
alert(alert_msg); 
<?php } ?> 
</script> 

ステータスメッセージを表示するためにalertを使用しました。それがうまくいくことを望みます。

あなたのフォームの隠れたスタイルを削除してください(その理由はどうでしたか)。

+0

ねえ。私はあなたのコードを試しましたが、それは壊れているようです。それはphpとして認識されず、ウェブサイト上にテキストとして表示されます。 .htmlと.phpとして試してみました。 –

+0

コードエディタを –

+0

で保存している場合は、ファイル形式をすべてのファイルに設定し、ファイル拡張子を.phpとして追加してください。保存されたファイルがexample.txt.phpにあるか、またはファイルがlocalhost(サーバー)フォルダに置かれていないようです。 –

関連する問題