php请求完成后激活标头

时间:2012-03-31 19:03:12

标签: php redirect header buffer

我目前正在开发一个网站,用户可以将图片上传到10mb。当用户按上传时,该文件存储在服务器上的目录中,然后通过使用标题重定向页面。请注意,我还将ob start()放在脚本的开头。我遇到的问题是标头重定向在请求完成之前激活,因此用户会被注销,或者发生内部服务器错误。删除大文件时会发生同样的事情。我已尝试删除重定向及其罚款,因此我确定这是问题所在。有没有办法在php请求(图片上传)完成后才能获得标题重定向?

谢谢!

编辑:代码示例

if (empty($errors)) {

  // Get the filename minus the file extension:
$filename = substr($image["name"], 0, strrpos($image["name"], "."));
// Append the appropriate extension
$filename .= $validMimes[$image['type']];
$location = "/home/shoplft/users/$user_id/$foldername/$filename";  
  move_uploaded_file($tmp_name, $location);




  if($secondcategory=="0") {


     mysql_query("INSERT INTO products (users_user_id, name, brand, city, country, store, website, price, keywords, productlocation, filename) VALUES ('$user_id', '$productname', '$productbrand', '$city', '$country', '$store', '$website', '$price', '$tagwords', '$location', '$filename')") or die('Invalid query: ' . mysql_error());

     $pic_id = mysql_insert_id();

     mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$firstcategory')");

     if(!empty($price)) {

        mysql_query("INSERT INTO svalues (products_products_id, products_users_user_id, svalue) VALUES ('$pic_id', '$user_id', '$price') ");

     }




  }



  else {

  mysql_query("INSERT INTO products (users_user_id, name, brand, city, country, store,   website, price, keywords, productlocation, filename) VALUES ('$user_id', '$productname', '$productbrand', '$city', '$country', '$store', '$website', '$price', '$tagwords', '$location', '$filename')") or die('Invalid query: ' . mysql_error());

  $pic_id = mysql_insert_id();

 mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$firstcategory')");
 mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$secondcategory')");

  if(!empty($price)) {

        mysql_query("INSERT INTO svalues (products_products_id, products_users_user_id, svalue) VALUES ('$pic_id', '$user_id', '$price') ");

     }  


  }

header("Location: product.php?id=$pic_id");//prolly should separate data processing from output

 }    

1 个答案:

答案 0 :(得分:0)

试试这个

if (empty($errors)) {

  // Get the filename minus the file extension:
$filename = substr($image["name"], 0, strrpos($image["name"], "."));
// Append the appropriate extension
$filename .= $validMimes[$image['type']];
$location = "/home/shoplft/users/$user_id/$foldername/$filename";
if(!@copy($tmp_name, $location))
{
$errors[] =  'Error';
}

if (empty($errors))
{




  if($secondcategory=="0") {


     mysql_query("INSERT INTO products (users_user_id, name, brand, city, country, store, website, price, keywords, productlocation, filename) VALUES ('$user_id', '$productname', '$productbrand', '$city', '$country', '$store', '$website', '$price', '$tagwords', '$location', '$filename')") or die('Invalid query: ' . mysql_error());

     $pic_id = mysql_insert_id();

     mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$firstcategory')");

     if(!empty($price)) {

        mysql_query("INSERT INTO svalues (products_products_id, products_users_user_id, svalue) VALUES ('$pic_id', '$user_id', '$price') ");

     }




  }



  else {

  mysql_query("INSERT INTO products (users_user_id, name, brand, city, country, store,   website, price, keywords, productlocation, filename) VALUES ('$user_id', '$productname', '$productbrand', '$city', '$country', '$store', '$website', '$price', '$tagwords', '$location', '$filename')") or die('Invalid query: ' . mysql_error());

  $pic_id = mysql_insert_id();

 mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$firstcategory')");
 mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$secondcategory')");

  if(!empty($price)) {

        mysql_query("INSERT INTO svalues (products_products_id, products_users_user_id, svalue) VALUES ('$pic_id', '$user_id', '$price') ");

     }


  }

header("Location: product.php?id=$pic_id");//prolly should separate data processing from output
 }
 }