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.