PHP使$ _FILES为空

时间:2014-07-30 09:08:58

标签: php ajax apache

我有一个表单可以发布带或不带图像的文本。  当我上传带有图像的文本时,之后我只想上传文本  没有任何图片,但$_FILES仍然填充了我上传的最后一张图片。  所以我的问题是如何在图片上传后使$_FILES为空  我已经尝试过了:

  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);"

现在它正常工作,感谢所有的帮助。

2 个答案:

答案 0 :(得分: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
 ?>   

答案 1 :(得分:0)

if (isset($_FILE)){
 $DataProcessed = DataProcessingFunction();
}

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

有关详细说明,请参阅:Unset uploaded files in PHP