Hi, i am trying to validate my upload form and struggling with a couple of validation methods. i want to not allow the user to upload the file larger than 5mb i have changed my php.ini file to 5M and done some coding to make it work but when i upload a file larger than 5mb i want the screen to echo something like "file too large to upload" but i dont know how to put that in. could someone advise please (code is below) my other question is how to check before my file is uploaded weather a file in the database has the same name. i am uploading images to a folder and storing the images path in the database please help it would be soo helpful! PHP: <?php $max_size=5*1024*1024; $filename =. basename($_FILES['uploaded_file']['name']); // Check if a file has been uploadedif(isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['size']<= $max_size){ // Make sure the file was sent without errors if($_FILES['uploaded_file']['error'] == 0) { $target_path = "images/"; $target_path = $target_path . basename( $_FILES['uploaded_file']['name']); if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploaded_file']['name']). " has been uploaded"; $dbLink = new mysqli('localhost', 'root', '', 'gallery'); if(mysqli_connect_errno()) { die("MySQL connection failed: ". mysqli_connect_error()); } // Gather all required data $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']); $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']); $size = intval($_FILES['uploaded_file']['size']); $image_path = $dbLink->real_escape_string($target_path); $gallery_type = $dbLink->real_escape_string($_POST['gallery_type']); //query to insert the data i had gathered into the database $query = "INSERT INTO `images` (`name`, `size`, `created`, `image_path`, `gallery_type_id`) VALUES ('{$name}', {$size}, NOW(), '{$image_path}', '{$gallery_type}')"; //executes the query $dbLink->query($query); } } else { echo 'Error! A file was not sent!'; }} // Echo a link back to the main page echo '<p>Click <a href="member-index.php">here</a> to go back</p>'; ?>
PHP: if ($_FILE['uploaded_file']['size'] > $max_size) echo "<b>File too large to upload</b>"; Put that as the one of the conditions over there. How come its hard for you when you can code so much. Also, for checking if the imagename is already in the database, use the SELECT query with the name of file you are uploading and if the mysql_num_rows returns value more than 0, you know the imagename is already present in the database.
If you want to take the uploaded filename use $_FILES['uploaded_file']['name'] .Using this you can compare the database filename. Use the following condition for checking the file size error. $_FILES['my_file']['error'] ==UPLOAD_ERR_INI_SIZE)||($_FILES['my_file']['error'] == UPLOAD_ERR_FORM_SIZE
How would I check the file name is in the database? I'm already checking if it is in a folder but that's not always acurate. Could you show me please.