2011-10-29 1 views
1

EDITED. index.html을 내부질문을 명확히하기 위해 메인 HTML에 변수를 전달하는 파일 업로드 양식

  1. 파일 업로드 양식을 :

    어디 싶어. (완료)

  2. 양식이 PHP 파일로 제출됩니다. (완료)
  3. PHP 파일은 파일을 올바른 위치로 이동시킵니다. (완료)
  4. index.html이 다시로드되고 PHP가 종료되었습니다. (done)
  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로 돌아갈 수 없다. 나는 반환이 index.php로 돌아 가야한다고 생각하지만 어떻게 든 그것은하지 않습니다. – Geolassi

관련 문제