0

I have a form to post text with or without an image. When i have uploaded text with an image and after that i only want text to upload without any image,but $_FILES is still filled with the last image i uploaded. So my question is how to make $_FILES empty after a image upload.
I already tried:

  unset($_FILES['file'][0])

 but i cant get it empty.



           <?php   
       session_start();

        include_once("conf/config.php");
        include_once("twitteroauth/twitteroauth.php");
        echo '<script> function clearform(){ document.getElementById("fileupl").value=" "; }<script>';
       $name = $_POST['txt1'];
       $myfile = $_FILES["fileup"]["name"];
       $connection = new TwitterOAuth($consumer_key,     
       $consumer_secret,$access_token,$accesstoken_secret);
       //print_r($_POST);
       var_dump($_FILES);
       $image = "";
       $errors = "";

     if(isset($_FILES["fileup"])){

        //echo 'jajajajajaja';
        $account = $connection->get('account/verify_credentials');
        /* KIJK OF ER EEN FOUT IS OPGETREDEN */
        if($_FILES["fileup"]["error"] > 0){ 
                    echo "Error occured ".$_FILES["fileup"]["error"];  
        }
        /* GEEN FOUT */ 
        $e = $_FILES['fileup']['tmp_name'];
        /* CONTROLEER PHP'S INGEBOUWDE ERROR CHECK VOOR UPLOADEN. */
        if($_FILES['fileup']['error']) echo $errors[$_FILES['file']['error']]; 
        $image = $e;
        /* ALS ER GEEN FOTO GESELECTEERD IS, POST ALLEEN DE TEKST */     
        if($image === "") {
            $paramss = array('status'  => $name);
            $post= $connection->post('statuses/update',$paramss);  
            exit();
        } 
        /* ANDERS TEKST MET FOTO */      
        $handle = fopen($image,"r");
        $image     = fread($handle,filesize($image));
        fclose($handle);
        $params = array(
            'media[]' => "{$image};type={$_FILES['file']['type']};filename={$_FILES['file']['name']}",
            'status'  => $_POST['txt1'],
            true, // use auth
            true  // multipart
        );

 $DataProcessed = $connection->post('statuses/update_with_media',$params, true);


if (isset($DataProcessed) && $DataProcessed){

$_FILES = array();        $_FILES['file']['name'] = null;
        $_FILES['file']['tmp_name'] = null;
        $_FILES['file']['error'] = null;
        $_FILES['file']['size'] = null;
        echo "data processed";
        var_dump($_FILES);  
$_POST = array(); // lets pretend nothing was posted        //




// Define the folder to clean
// (keep trailing slashes)
$captchaFolder  = 'd:/windows/temp/';

// Filetypes to check (you can also use *.*)
$fileTypes      = '*.*';

// Here you can define after how many
// minutes the files should get deleted
$expire_time    = 1; 

// Find all files of the given file type
foreach (glob($captchaFolder . $fileTypes) as $Filename) {

    // Read file creation time
    $FileCreationTime = filectime($Filename);

    // Calculate file age in seconds
    $FileAge = time() - $FileCreationTime; 

    // Is the file older than the given time span?
    if ($FileAge > ($expire_time * 6)){

        // Now do something with the olders files...

        print "The file $Filename is older than $expire_time minutes\n";

        // For example deleting files:
        unlink($Filename);
        }

        `enter code here`}

        exit();
       } 
       } 


       ?>
       and i use this in my form
 onsubmit="setTimeout(function(){clearform()},3000);"

and now it is working correctly thanks for all the help.

robert
  • 17
  • 5

2 Answers2

0
I have assume that youe form like this 
 <form id="frmTest" name="frmTest" action="post.php" method="post" enctype="multipart/form-data">
   <input type="test" id="imgName" name="imgName">
   <input type="file" id="uploadImg" name="uploadImg">
   <input type="submit" value="submit" name="btnsub" id="btnsub">
</form>

Now you can get value in Post.php after submit form
 <?php
       echo "<pre>";print_r($_POST); //it show image name if you add other wise show null
       echo "<pre>";print_r($_FILES);//it show upload image if you can upload otherwise show null
 ?>   
prash.patil
  • 687
  • 8
  • 7
  • i am not sure if you understand what i mean but i dont want the posted value i want the array $_FILES be empty after a image is uploaded.i tried the following $_FILES['file'][0] = array(); after the line that is uploading the image but now there is no upload of image so it is cleared before upload,i want it to clear after the upload. this is the line that does the upload: $post= $connection->post('statuses/update_with_media',$params, true); print_r($post); and after that line i have $_FILES['file'][0] = array(); – robert Jul 30 '14 at 09:27
  • ok suchit tried that but it is not clearing this is what i tried: `$DataProcessed = $connection->post('statuses/update_with_media',$params, true); if (isset($DataProcessed) && $DataProcessed){ //header("Location: indexx.php"); exit(); }` – robert Jul 30 '14 at 11:07
  • ok it is working now but the problem still exists, i made sure to empty the $_FILES array but the image from the first tweet is also uploaded with the second tweet so is there another place where it stores this array or what. – robert Jul 30 '14 at 20:16
0
if (isset($_FILE)){
 $DataProcessed = DataProcessingFunction();
}

if (isset($DataProcessed) && $DataProcessed){
  header("Location: /path/to/form/page.php");
  exit();
}

For more clarification see this:Unset uploaded files in PHP

Community
  • 1
  • 1
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44