2011-10-29 7 views
1

EDITED:質問を明確にします。ファイルをアップロードするフォーム。メインHTMLに変数を提供します。

  1. のindex.html内のファイルのアップロードフォームを:私は取得したい

    (完了)

  2. フォームはPHPファイルに送信されます。 (完了)
  3. PHPファイルは、ファイルを適切な場所に移動します。 (完了)
  4. index.htmlがリロードされ、PHPが終了しました。
  5. index.htmlは、後で使用するためにPHPからアップロードされたファイルの場所を取得します。 (元に戻す)

私は私が戻ったindex.htmlにPHPからパラメータを与えるか、知っていない、または問題がどのようにPHPとHTMLの情報交換、私は理解していないです。

このような機能がAjaxで書かれていて、私のindex.htmlに「出かける」必要はありません。

私は、HTML内でこの形式があります。

<form method="post" enctype="multipart/form-data" action="uploader.php"> 
<input type="file" name="uploadedfile"/> 
<input type="submit" name="submit" class="button" value="Submit"></button> 
</form> 

をそして、それが呼び出すPHPは、次のようになります。

<?php 
$target_path = "uploads/"; 
$target_path = $target_path . ($_FILES["uploadedfile"]["name"]); 
if(move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], $target_path)) { 
echo "Success!" 
} else{ 
echo "There was an error uploading the file, please try again!"; 
} 
header('Location: http://localhost/index.html'); 
exit(); 
?> 

提出押すと、ファイルが正しくサーバーにアップロードされます。

+2

質問を明確にすることはできますか? – svinto

+0

これはコイルタイプの質問 – leon

+0

あなたの質問はあなたが明確にしない限り閉じられます。 – Nasreddine

答えて

0

index.htmlファイルをPHPファイルに変更して、埋め込まれたPHPコードを持つようにして、次のような文章を書くことができます。

これはあなたにいくつかのアイデアを与える

<?php 
$target_path = "uploads/"; 
$target_path = $target_path . ($_FILES["uploadedfile"]["name"]); 
if(move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], $target_path)) { 
echo "Success!" 
} else{ 
echo "There was an error uploading the file, please try again!"; 
} 

//The following code POSTs data to index.php 
$params = array('http' => array(
      'method' => 'POST', 
      'content' => array('target' => $target_path) 
     )); 
$ctx = stream_context_create($params); 
$fp = @fopen('index.php', 'rb', false, $ctx); 
if (!$fp) { 
    throw new Exception("Problem with index.php, $php_errormsg"); 
} 
$response = @stream_get_contents($fp); 
if ($response === false) { 
    throw new Exception("Problem reading data from $url, $php_errormsg"); 
} 
return $response; 
?> 

希望uploader.phpのindex.php

<form method="post" enctype="multipart/form-data" action="uploader.php"> 
<input type="file" name="uploadedfile"/> 
<input type="submit" name="submit" class="button" value="Submit"></button> 
</form> 

<?php 
if($_POST['target']){ 
    //Do stuff with the target 
} 
?> 

+0

答えをありがとう!私はそれをテストしましたが、私はuploader.phpにこだわりました。私はindex.phpに戻ることができません。私はreturnをindex.phpに戻すべきだと思いますが、どういうわけかそれはしません。 – Geolassi

関連する問題