Messages - The Code Queryer [ switch to compact view ]

Pages: prev1 2 3 4 5 [6] 7next
26
Developer's Corner / if(file_exists..) is Failing!
« on: April 06, 2019, 12:47 PM »
Php Whizes!

A certain video file exists in a certain directory on my hdd. I get the script to upload the same file again expecting to see error message (based on my coding) that the file has already been uploaded. But, I see no error appearing. Why is that ? Trouble in this line:
if(file_exists("$directory_path . $user/ . $file_name")) //THIS LINE IS NOT GIVING THE ERROR THAT FILE HAS ALREADY BEEN UPLOADED. INSTEAD GIVES THE ECHO THAT IS 26 LINES BELOW HERE: "Your Video File \"$file_name\" has been uploaded successfully!"

Context of the script:

//Feed Id Verification Video File Upload Directory path.
$directory_path = "uploads/videos/id_verifications/";
//Make Directory under $user in 'uploads/videos/id_verifications' Folder.
if(!is_dir($directory_path . $user))
{
$mode = "0777";
mkdir($directory_path . $user, "$mode", TRUE); //IS THIS LINE CORRECT ?
}

//Grab Uploading File details.
$Errors = Array(); //SHOULD I KEEP THIS LINE OR NOT ?
$file_name = $_FILES["id_verification_video_file"]["name"];
$file_tmp = $_FILES["id_verification_video_file"]["tmp_name"];
$file_type = $_FILES["id_verification_video_file"]["type"];
$file_size = $_FILES["id_verification_video_file"]["size"];
$file_error = $_FILES['id_verification_video_file']['error'];

//Grab Uploading File Extension details.
$file_extension = pathinfo($file_name, PATHINFO_EXTENSION);
if(file_exists("$directory_path . $user/ . $file_name")) //THIS LINE IS NOT GIVING THE ERROR THAT FILE HAS ALREADY BEEN UPLOADED. INSTEAD GIVES THE ECHO THAT IS 26 LINES BELOW HERE: "Your Video File \"$file_name\" has been uploaded successfully!"
//if(file_exists($directory_path . $user . '/' . $file_name)) //THIS LINE IS NOT GIVING THE ERROR THAT FILE HAS ALREADY BEEN UPLOADED. INSTEAD SHOWING BLANK WHITE PAGE.
{
$Errors[] = "Error: You have already uploaded a video file to verify your ID!";
exit();
    }

I need help where you see me asking you a question on my comments (on my above code).

27
Php Lovers,

You're welcome to show me a code sample using file_info() to only accept mp4 and .wav video files. This way, from your sample others like me can learn. :Thmbsup:

28
Php Seniors,

Why is this line malfunctioning ?
if(!array_key_exists($file_extension, $allowed_file_extensions)) die("Error: Select a valid video file format. Select an Mp4 file.");

I allowed the file type "mp4" and then fed the script (trying to uploade an mp4 file) an mp4 file.
I should get mssg upload successful and only get echoed errors if the selected file is not a video file. Since, I selected a video file to upload via the html form then I should not get any errors echoed. Common sense!
But guess what ? I get error message echoed: "Select a valid video file format. Select an Mp4 file."

Why is that ?

Here my code:
$file_name = $_FILES["id_verification_video_file"]["name"];
$file_tmp = $_FILES["id_verification_video_file"]["tmp_name"];
$file_type = $_FILES["id_verification_video_file"]["type"];
$file_size = $_FILES["id_verification_video_file"]["size"];
$file_error = $_FILES['id_verification_video_file']['error'];

$file_extension = pathinfo($file_name, PATHINFO_EXTENSION);
if(file_exists("$directory_path . $user/ . $file_name"))
{
$Errors[] = "Error: You have already uploaded a video file to verify your ID!";
exit();
    }
else
{
//Feed allowed File Extensions List.
$allowed_file_extensions = array('mp4');

//Feed allowed File Size.
$max_file_size_allowed_in_bytes = 1024*1024*1; //Allowed limit: 100MB.
$max_file_size_allowed_in_kilobytes = 1024*1;
$max_file_size_allowed_in_megabytes = 1;

$max_file_size_allowed = "$max_file_size_allowed_in_bytes";

//Create a fileinfo resource.
$finfo = finfo_open(FILEINFO_MIME_TYPE);
//Apply the FileInfo resource and the finfo_file() function to a given file.
$mime = finfo_file($finfo,$file_name);
//Close the fileinfo resource.
finfo_close($finfo);

//Verify File Extension.
if(!array_key_exists($file_extension, $allowed_file_extensions)) die("Error: Select a valid video file format. Select an Mp4 file.");


The following code fails to move the FILE to it's intended place:
//Move uploaded File to newly created directory on the server.
move_uploaded_file($file_tmp, $directory_path . "$user/" . $file_name); //IS THIS LINE CORRECT ?
//Notify user their Id Verification Video File was uploaded successfully.
echo "Your Video File \"$file_name\" has been uploaded successfully!";
exit();

Before, attempted this with no luck too:

//Move uploaded File to newly created directory on the server.
move_uploaded_file("$file_tmp", "$directory_path . $user/ " . "$file_name"); //IS THIS LINE CORRECT ?
//Notify user their Id Verification Video File was uploaded successfully.
echo "Your Video File \"$file_name\" has been uploaded successfully!";
exit();

29
Php Gurus,

When I select a video mp4 file and get the File_Type to echo it then it echoes blank. Why is that ?
$file_type = $_FILES["id_verification_video_file"]["type"];
echo "File Type: $file_type<br>";

The form I mentioning below incase you wondering whether I mispelled the item name.:
<form METHOD="POST" ACTION="" enctype="multipart/form-data">
<fieldset>
<p align="left"><h3><?php $site_name ?> ID Video Verification Form</h3></p>
<div class="form-group">
<p align="left"<label>Video File: </label>
<input type="file" name="id_verification_video_file" id="id_verification_video_file" value="uploaded 'Id Verification Video File.'"></p>
</div>
</fieldset>
<p align="left"><button type="submit" class="btn btn-default" name="id_verification_video_file_submit">Submit!</button></p>
</form>

If I select a .php file then it manages to show the file type. Only shows blank on the mp4 file.

30
DC Member Programs and Projects / Re: GDPR Blocking (PHP Edition)
« on: April 06, 2019, 12:23 PM »
There is an expensive web service that makes your website GDPR-compliant by adding a JavaScript that redirects visitors from the EU to an error page.

This is my attempt to provide a simple and free PHP script for those who want to achieve the same result on server side.

Code: PHP [Select]
  1. <?php
  2. /*
  3.   Copyright © 2018 tux. <[email protected]>
  4.   This work is free. You can redistribute it and/or modify it under the
  5.   terms of the Do What The Fuck You Want To Public License, Version 2,
  6.   as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
  7.  
  8.   GDPR SHIELD [PHP draft v1]
  9.  
  10.   Prerequisites:
  11.   * PHP
  12.   * GeoIP as described here: http://php.net/manual/en/geoip.requirements.php
  13.  
  14.   Usage:
  15.   <?php require("gdprshield.php"); ?>
  16.   <!doctype html>
  17.   <html>
  18.     ...
  19.   </html>
  20. */
  21. $disallowed_countries = ["BE", "BG", "CZ", "DK", "DE", "EE", "IE", "EL", "ES", "FR", "HR", "IT", "CY", "LV", "LT", "LU", "HU", "MT", "NL", "AT", "PL", "PT", "RO", "SI", "SK", "FI", "SE", "UK"];
  22. $ip = $_SERVER["REMOTE_ADDR"];
  23. if (in_array(geoip_country_code_by_name($ip), $disallowed_countries)) {
  24.     die("Your country does not want you to be here.");
  25.  
  26.     /*
  27.       The above code displays a plain error text.
  28.       If you prefer to redirect to a full-featured HTML page,
  29.       you could delete it and use a Location header instead:
  30.     */
  31.     header("Location: GDPR_blocked.php", true, 451);
  32. }
  33. ?>

Enjoy. And feel free to extend/fix/implement it as you wish.

According to this link:
https://www.php.net/...oip.requirements.php

You need c library installed. Not sure how to install that on my cPanel. I was looking for something with php alone.

Pages: prev1 2 3 4 5 [6] 7next
Go to full version