Jump to content

how can i replace a file


nosmas

Recommended Posts

I was just building a page that enables the admin to upload news with multiple images,But i do not know how to replace the images while editing the news.

 

here is the code that uploads the news

 

<?php

 

if(!isset($_POST['multipic'])){

  filter_file($_FILES['picture']['type'],$_FILES['picture']['size']);

if($_FILES['picture']['error']){

        $msg="the picture".$_FILES['picture']['name']."has got an error";

        header("Location:uploadNews.php?msg=$msg");

        exit(0);

        }

 

$title=strip_tags($_POST['title']);

    $detail=strip_tags($_POST['detail']);

    $author=strip_tags($_POST['author']);

    $query2="INSERT INTO news(title,detail,author) VALUES('$title','$detail','$author')";

      mysql_query($query2)or die(mysql_error()."lilne-30");

    $news_id=mysql_insert_id();

$path="upload/news".$news_id.$_FILES['picture']['name'];

$name=$_FILES['picture']['name'];

    move_uploaded_file($_FILES['picture']['tmp_name'],$path );

    $query1="INSERT INTO picture(pic_path,pic_caption) VALUES('$path','$name') ";

    mysql_query($query1) or die(mysql_error()."line-24");

    $pic_id=mysql_insert_id();

   

    $query3="INSERT INTO news_picture VALUES($news_id,$pic_id)";

    mysql_query($query3) or die(mysql_error()."line-33");

 

 

 

 

 

 

 

if(isset($_POST['add_pic']) && !empty($_POST['add_pic'])&&($_POST['add_pic']!="0")){

 

    ?>

    <form action="<?echo $_SERVER[php_SELF]?>" method="post" enctype="multipart/form-data">

        <input type="hidden" value="<?echo $_POST['title']?>" name="title" /><br />

        <input type="hidden" value="<?echo $_POST['detail']?>" name="detail" /><br />

        <input type="hidden" value="<?echo $_POST['add_pic']?>" name="num_pic" /><br />

        <input type="hidden" value="<?echo $news_id?>" name="news_id" /><br />

        <input type="hidden" value="1" name="multipic" />

       

<?

        for($i=0;$i<$_POST['add_pic'];$i++){

      $name="pic".$i;

      echo "picture".$i+1;

    ?>

      <input type="file"  name="<?echo $name?>" /><br />

    <?

              }

        ?>

      <input type="submit" value="Upload" />

      </form>

    <?

      }else{

     

        die (":-)  done");

      }

 

}else{

$news_id=$_POST['news_id'];

for($i=0;$i<$_POST['num_pic'];$i++){

 

$name='pic'.$i;

 

filter_file($_FILES[$name]['type'],$_FILES[$name]['size']);

if($_FILES[$name]['error']){

        $msg="the picture".$_FILES[$name]['name']."has got an error";

        header("Location:uploadNews.php?msg=$msg");

        exit(0);

        }

 

$path="upload/".$news_id.$i.$_FILES[$name]['name'];

 

move_uploaded_file($_FILES[$name]['tmp_name'],$path );

 

$query1="INSERT INTO picture(pic_path,pic_caption) VALUES('$path','$name') ";

mysql_query($query1) or die(mysql_error()."line-82");

$pic_id=mysql_insert_id();

 

$query2="INSERT INTO news_picture VALUES($news_id,$pic_id)";

mysql_query($query2) or die(mysql_error()."line-86");

 

}die(":-)donnne");

 

}

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.