I've the following PHP script launching on click of a submit form button :
<?php
//header('Location: video_download.html');
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["logo"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$new_name = 'logo'.'.'.$imageFileType;
$target_file = $target_dir . $new_name;
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["logo"]["tmp_name"]);
if($check == false) {
echo "File is not an image.\n";
$uploadOk = 0;
}
}
// Check file size
if ($_FILES["logo"]["size"] > 5000000) {
echo "Sorry, your file is too large.\n";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "png") {
echo "Sorry, only PNG files are allowed.\n";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.\n";
if (file_exists("config.txt")) {
unlink("config.txt");
}
if (file_exists("upload/logo.png")) {
unlink("upload/logo.png");
}
// if everything is ok, try to upload file & create config.txt
} else {
if (move_uploaded_file($_FILES["logo"]["tmp_name"], $target_file)) {
$template = $_POST['template'] . "\n";
$club = $_POST['club'] . "\n";
$stats = $_POST['stats'];
if (file_exists("config.txt")) {
unlink("config.txt");
}
file_put_contents("config.txt", $template . $club . $stats);
echo '<meta http-equiv="Refresh" content="0; url=video_download.html">';
shell_exec("./blender_lite/bin/blender -b -P blender.py");
exit();
} else {
echo "There was an error uploading your file.\n";
exit();
}
}
?>
I want it to redirect the browser to another html page after executing all script's content. I've tried two different approach : the header (commented in the above code) and echoing an html meta tag with a redirection.
Both doesn't work for unknown reasons and I can't find it (already passed many hours trying to find other methods and resolve the issue without success, so I guess it's kind of a specific issue with a part of my php script). Any help is welcome, thanks :)
You would use the header function.
header("Location: http://www.website.com/page.php");
exit();
Replace the part of your code where you echo the meta refresh with the header location command and place it at the end of your logic. This will only redirect when everything else is complete then.
Example:
if (move_uploaded_file($_FILES["logo"]["tmp_name"], $target_file)) {
$template = $_POST['template'] . "\n";
$club = $_POST['club'] . "\n";
$stats = $_POST['stats'];
if (file_exists("config.txt")) {
unlink("config.txt");
}
file_put_contents("config.txt", $template . $club . $stats);
shell_exec("./blender_lite/bin/blender -b -P blender.py");
header('Location: video_download.html'); // <-- put redirect here
exit();
} else {
echo "There was an error uploading your file.\n";
exit();
}
Found out that the redirection worked fine as I did on my first post. The issue was that PHP waits for the shell_exec to be over. Thus, since I was launching a blender rendering with shell_exec, it was taking several minutes to end so I thought the redirection was not working.
Using exec('bash -c "exec nohup setsid ./blender_lite/bin/blender -b -P blender.py > /dev/null 2>&1 &"');
instead of shell_exec("./blender_lite/bin/blender -b -P blender.py");
so the PHP script doesn't wait for the command to end is the solution, the redirection is now immediate.
Related
On a PHP page, I call an upload function that contains the standard PHP upload procedure. After I call the function, I do a redirect (tried with either window. location or header ()).
The strange thing is that everything works fine a couple of times, then it would not upload anymore (uploadOK won't be 0 either). It would just not move the file onto the server.
Then, I would take out the redirect and the upload would start working again. I put the redirect back in, the upload will still work a couple of times then stop again... Do you have any idea why?
Another strange thing is that, even when it doesn't work, the upload function still returns the correct path+filename, but it would not echo "File ... was uploaded".
I suspect that the problem might be in the move_uploaded_file() function... but it would not return 0, because "Error..." would not be echoed.
Without calling the redirect after the upload, it uploads fine every time.
PHP page:
$_SESSION["temp_file_name"]="../".UploadFisier($_FILES["fileToUpload"], $_SESSION["ID_CLASA"]."_".$id_item."_temp_".UserIdLogat($dbocr)."_", "../teme/", "");
header("Location: trimite_tema_script.php");
The Upload function:
function UploadFisier($file, $sufix, $target_dir, $maxsize)
{
if ($maxsize=="") { $maxsize=3000000; }
if ($target_dir=="") { $target_dir="../upload/"; }
$target_empty_file=$target_dir.$sufix;
$target_file = $target_empty_file.basename($file["name"]);
$uploadOk = 1;
if ($target_file != $target_empty_file)
{
if ($file["size"] > $maxsize)
{
echo "file too large";
$uploadOk = 0;
}
}
else
{
echo "no file selectec.";
$uploadOk = 0;
}
if ($uploadOk == 1)
{
//we overwrite
if (file_exists($target_file))
{
unlink($target_file);
}
if (move_uploaded_file($file["tmp_name"], $target_file))
{
echo "File ". basename( $file["name"]). " was uploaded.";
//we output the path and filename without the "../" at the beginning
$linksave=substr($target_file, 3);
}
else
{
echo "Error....";
}
}
echo "uploadOk ".$uploadOk;
return $linksave;
}
Check the file_uploads value in PHP.ini and confirm that is something like this
file_uploads = On
Wrap a try/catch in your code. maybe you will find out more about the error.
function UploadFisier($file, $sufix, $target_dir, $maxsize)
{
try {
// your code
} catch (\Exception $e) {
echo $e->getMessage();
die();
}
}
first i've to say, that i'm totally new to cURL. But I've just code some lines with curl and PHP to Upload some XML files.
The following Code is working very good. (just looked around and build up a simple working solution)
Now i've got the issue to use the curl --Data-Binary option instead of the -F (Form) option.
The following Code is in the php-File (curlupload.php):
<?php
#$authcode = htmlspecialchars($_POST["acode"]);
if ($authcode == "1234"){
echo "AuthCode ok\n";
$upstatus = "1";
$uploadpath = "files/";
$filedata = $_FILES['file']['tmp_name'];
$target_file = $uploadpath . basename($_FILES["file"]["name"]);
#echo "Acode:" . $authcode . "\n";
#echo "filedata= ".$filedata."\n";
echo "target_file= ".$target_file."\n";
#echo "1 upstatus= ".$upstatus."\n";
if (strpos($target_file, ".xml") == true) {
$upstatus = "1";
echo "2 upstatus= ".$upstatus."\n";
if (file_exists($target_file)) {
echo "Sorry, file already exists.\n";
$upstatus = "0";
}
#echo "4 upstatus= ".$upstatus."\n";
if ($upstatus == "1") {
if ($filedata != '')
copy($filedata,$target_file);
echo "\n-----\n";
echo "success\n";
}
}else{
$upstatus = "0";
#echo "3 upstatus= ".$upstatus."\n";
echo "Filenamecheck Failed.\n";
}
if ($upstatus == "0") {
echo "Sorry, your file was not uploaded.\n";
}
}else{
echo "NO.";
}
?>
my cURL command for uploading a file is the following:
curl -v -F 'file=#/root/testfile01.xml' -F 'acode=1234' http://webserver/upload2/curlupload.php
Now i'm going to use the -d and --data-binary option like this:
curl --data-binary "file=#testfile02.xml" --data "acode=1234" http://webserver/upload2/curluploadtest.php
with the 2nd above command i receive the following messages:
AuthCode ok
target_file= files/
Filenamecheck Failed.
Sorry, your file was not uploaded.
so, the variables got no input - Can somebody please tell me, what i've got to change in the PHP file, so, that the databinary option used by curl is going to work?
Thanks a lot for your helping toughts!
Going out of my mind with php unlinking
Here is my delete file script
$pictures = $_POST['data'];
//print_r ($pictures);
$imageone = $pictures[0];
$filename = "file:///Users/LUJO/Documents/CODE/REVLIVEGIT/wp-content/uploads/dropzone/" . $imageone;
echo $filename;
if (is_file($filename)) {
chmod($filename, 0777);
if (unlink($filename)) {
echo 'File deleted';
} else {
echo 'Cannot remove that file';
}
} else {
echo 'File does not exist';
}
The above does not work, error response is file does not exist
however if i change the filename path to this (the echo data from the echo above)
$filename = "file:///Users/LUJO/Documents/CODE/REVLIVEGIT/wp-content/uploads/dropzone/1420291529-whitetphoto.jpeg "
works fine and deletes the image.
Why can i not use the $imageone variable?
Do a print_r($pictures) to see if $pictures[0] is indeed the filename you're looking for.
Also note that if $pictures[0] is "//windows/*" you'll loose your windows if the user running PHP has administrative rights... so just using $pictures=$_POST["data"] is very VERY unsafe!
I have a secure file upload function that's part of my website
and I'm using an antivirus to help me checking the file a user trying to upload.
This is my uploadprocess.php file
$target_tmp = "D:\avscan\u\\";
$file = basename( $_FILES['uploaded_file']['name']) ;
if($file != "")
$_SESSION['file'] = $file;
$target = 'C:\xampp\htdocs\ssd\Uploads\\';
$file_path = $target_tmp.$file;
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path))
{
$safe_path = escapeshellarg($file_path);
$command = 'scancl'. $safe_path. ' --stdout';
$out = '';
$int = -1;
$output = exec($command, $out, $int);
echo "The output is" .$output;
echo $int;
exit(0);
//Checking for Virus.
if ($int == 0) {
$target = $target.$file;
//echo $target; exit(0);
copy($file_path, $target);
$uploaded = "The file ". $_SESSION['file']. "has been uploaded";
$clean = 'File is Clean.';
$_SESSION['status'] = $clean;
$_SESSION['upload'] = $uploaded;
header("location: ../upload.php");
exit(0);
}
// File is a virus.
else {
$mal = 'Contains Malware';
$deny_up = "Unable to Upload Your File!";
$_SESSION['status'] = $mal;
$_SESSION['upload'] = $deny_up;
header("location: ../upload.php");
exit(0);
}
}
else
{
echo "SORRY, There was a Problem Uploading Your File."; exit(0);
$err_upload = "SORRY, There was a Problem Uploading Your File.";
$_SESSION['err'] = err_upload;
header("location: ../upload.php");
exit(0);
}
It prints me value of 1 for the $int for all files (malicious and non ones) This is my second try with a different AV now I'm using Avira and before I was using clamscan
can someone share me some hints, and tell me what's going on
PS the system is installed on XAMPP if that makes any difference
Can you be more specific about what's not working here? In theory what you doing seems fine at least for ClamAV since it has these return codes (from man clamscan):
RETURN CODES
0 : No virus found.
1 : Virus(es) found.
2 : Some error(s) occured.
Maybe it want to log the output of the exec call, if you are not getting the exit code you expect the reason should be in the output (like missing a command line flag).
I have a php page that is supposed to store the uploaded image to my server. When I run this, I get the "Upload successful" message, but the picture has not been uploaded.
What could it be?
update: can people please leave a comment as to why they down vote my question. I'm new here and I dont know why this question got down voted. thanks
<?
if(!empty($_FILES['uploaded_file'])) {
if ($_FILES['uploaded_file']['error'] > 0 )
echo "Error: " . $_FILES['uploaded_file']['error'] . "<br />";
else{
// Add the original filename to target path.
$target_path = 'MemberPics\\user'.$userid.'.jpg' ;
$success = move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path);
if(!$success) {
echo "There was an error uploading the file, please try again!";
}else {
echo "Upload successful, please go back to your home page";
}
}
}
?>
I believe the problem you are running into is that you are saving the image in an incorrect location (An invalid one from the looks of your link syntax).
Either of these should work:
$target_path = 'MemberPics/user'.$userid.'.jpg' ;
or
move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], "MemberPics/user" . $_FILES["uploaded_file"]["name"]);