2012-01-06 7 views
0

イメージをアップロードして表示する際に私のコードに問題があります。私はヘッダー機能を使用しましたが、警告とエラーを出して残念ながらアップロードプロセスが完了した後にページをリダイレクトする予定ですアップロードに失敗しました。どうすれば削除できますか?ここで私はまた私のギャラリーに問題があるヘッダーエラーを取り除く方法

<?php 

//connect to the database// 
$con = mysql_connect("localhost","root", ""); 
if(!$con) 
{ 
die('Could not connect to the database:' . mysql_error()); 
echo "ERROR IN CONNECTION"; 
} 

$sel = mysql_select_db("imagedatabase"); 
if(!$sel) 
{ 
die('Could not connect to the database:' . mysql_error()); 
echo "ERROR IN CONNECTION"; 
} 
//file properties// 

$file = $_FILES['image']['tmp_name']; 

echo '<br />'; 

/*if(!isset($file)) 
    echo "Please select your images"; 

else 
{ 
*/for($count = 0; $count < count($_FILES['image']); $count++) 
{ 
//$image = file_get_contents($_FILES['image']['tmp_name']); 
$image_desc[$count] = addslashes($_POST['imageDescription'][$count]); 
$image_name[$count] = addslashes($_FILES['image]']['name'][$count]); echo '<br \>'; 
$image_size[$count] = @getimagesize($_FILES['image']['tmp_name'][$count]); 
$error[$count] = $_FILES['image']['error'][$count]; 

if($image_size[$count] === FALSE || ($image_size[$count]) == 0) 
    echo "That's not an image"; 
else 
{ 

// Temporary file name stored on the server 
$tmpName[$count] = $_FILES['image']['tmp_name'][$count]; 

    // Read the file 
    $fp[$count] = fopen($tmpName[$count], 'r'); 
    $data[$count] = fread($fp[$count], filesize($tmpName[$count])); 
    $data[$count] = addslashes($data[$count]); 
    fclose($fp[$count]); 


// Create the query and insert 
// into our database. 

$results = mysql_query("INSERT INTO images(description, image) VALUES     ('$image_desc[$count]','$data[$count]')", $con); 

    if(!$results) 
    echo "Problem uploding the image. Please check your database"; 
    //else 
    //{ 
     echo ""; 
    //$last_id = mysql_insert_id(); 
    //echo "Image Uploaded. <p /> <p /><img src=display.php? id=$last_id>"; 
    //header('Lcation: display2.php?id=$last_id'); 
     } 
    //} 
} 


mysql_close($con); 
header('Location: fGallery.php'); 
?> 

ヘッダー機能は、おそらくここに...ギャラリーになるだろう別のページに私を指示したコード..です

<?php 

//connect to the database// 
    mysql_connect("localhost","root", "") or die(mysql_error()); 
    mysql_select_db("imagedatabase") or die(mysql_error()); 

    //requesting image id 




    $image = mysql_query("SELECT * FROM images ORDER BY id DESC"); 


    while($row = mysql_fetch_assoc($image)) 
    { 
     foreach ($row as $img) echo '<img src="img.php?id='.$img["id"].'">'; 
    } 

    mysql_close(); 


?> 

...コードです。いくつかの助けは素晴らしいでしょう!ありがとう! :

+0

エラーは何ですか? – Crontab

+0

あなたが持っているエラーを含めてください、あなたのギャラリーに問題がある場合、それらの問題を説明しなければなりません。 –

+2

[PHPで既に送信されたヘッダー]の複製が可能です(http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) – mario

答えて

4

出力を生成する他のechoまたはdieコールの前に、header()関数を呼び出す必要があります。

出力が必要な場合は出力をバッファリングすることができますが、出力がユーザーに表示されないため、出力に差は生じません。ブラウザはリダイレクトを読み取り、2番目のページに移動します。


<?php 

//connect to the database// 
$con = mysql_connect("localhost","root", ""); 
if(!$con) { 
// this output is okay the redirect will never be reached. 
die('Could not connect to the database:' . mysql_error()); 
// remember after a die this message will never be shown! 
echo "ERROR IN CONNECTION"; 
} 

$sel = mysql_select_db("imagedatabase"); 
if(!$sel) { 
die('Could not connect to the database:' . mysql_error()); 
echo "ERROR IN CONNECTION"; // same here with the die! 
} 
//file properties// 

$file = $_FILES['image']['tmp_name']; 

// OUTPUT 
// echo '<br />'; 

// removed out commented code 

for($count = 0; $count < count($_FILES['image']); $count++) 
{ 
$image_desc[$count] = addslashes($_POST['imageDescription'][$count]); 
$image_name[$count] = addslashes($_FILES['image]']['name'][$count]); 
// OUTPUT 
// echo '<br \>'; 
$image_size[$count] = @getimagesize($_FILES['image']['tmp_name'][$count]); 
$error[$count] = $_FILES['image']['error'][$count]; 

if($image_size[$count] === FALSE || ($image_size[$count]) == 0) 
// you may better use a die if you want to prevent the redirection 
    echo "That's not an image"; 
else 
{ 

// Temporary file name stored on the server 
$tmpName[$count] = $_FILES['image']['tmp_name'][$count]; 

    // Read the file 
    $fp[$count] = fopen($tmpName[$count], 'r'); 
    $data[$count] = fread($fp[$count], filesize($tmpName[$count])); 
    $data[$count] = addslashes($data[$count]); 
    fclose($fp[$count]); 


// Create the query and insert 
// into our database. 

$results = mysql_query("INSERT INTO images(description, image) VALUES     ('$image_desc[$count]','$data[$count]')", $con); 

    if(!$results) // use die 
    echo "Problem uploding the image. Please check your database"; 
// OUTPUT 
//  echo ""; 
     } 
} 


mysql_close($con); 
header('Location: fGallery.php'); 
?> 

私はあなたのためにすべての出力をマークしても、すべてのoutcomments行を削除の上に。

+1

die関数の前に呼び出す必要はありません。 die関数が呼び出されると、スクリプトは終了し、ヘッダ関数は決して実行されません。 –

+0

アップロードが成功した後、ページを次のページにリダイレクトするにはどうすればよいですか?ヘッダー関数を使用するだけですか?または別の方法がありますか? – SimonCode

+1

私が知っている限りでは、3つの可能性があることが分かります。ここでは、 'header()'はIMHOであり、HTML とjavascript' location.replace( "url") 'または' location.href = "url" 'を使用します。どのようにヘッダ・ステートメントの前に 'echo'のような出力をすべて削除したのでしょうか?また、ob_start()関数を使用して、ror_masterリンクを参照することもできます。 – rekire

2

ヘッダー機能の前に<br />を印刷したため、ヘッダーエラーが発生しました。ヘッダー機能を使用するには、その前に情報を印刷することはできません。だからあなたはエラーを受けている。

ギャラリーについてforeachループは不要です。コードを次のように変更することができます。

+0

i '
'を削除しましたが、まだヘッダの警告があります.. – SimonCode

+0

警告の前に印刷されているものを表示してください。 "ERROR IN CONNECTION"が表示された場合は、データベース接続に問題があります。 –

+0

ありがとう..もう消えてしまった。 :D – SimonCode

1

ob_start()を使用してバッファにデータを取得できます。

+0

助けてくれてありがとうror_master:D – SimonCode

関連する問題