Search the Community
Showing results for tags '$_files'.
-
I have an index.php file which includes my form and code to move the user's uploaded file to s3. My HTML form calls a js function sendEmails() which makes an AJAX request to another php script dbSystem() to validate the emails input and add it to a database. Everything is working except that the php code in my index.php file (at the very bottom) does not execute. It's supposed to execute when the user uploads a file and presses submit but it doesn't go into the if statement. I tried putting the $fileName = basename($_FILES["fileName"]["name"]) statement before the if statement but I get an undefined index error. I put my a comment in my code to show which if statement I am talking about. This is my HTML code in index.php: <form action="javascript:void(0)" method="POST" id="files" enctype="multipart/form-data"> <label class="col-md-4 col-form-label text-md-right">Select File: <span class="text-danger">*</span></label> <input type="file" id="userFile" name="fileName" style="cursor: pointer; max-width: 170px;" onchange="enableBtn()"> <label class="col-md-4 col-form-label text-md-right">Authorized Users: <span class="text-danger">*</span></label> <input placeholder="Enter e-mail(s) here..." id="req" autocomplete="off"/> <button id="submitBtn" name="submitBtn" class="<?php echo SUBMIT_BUTTON_STYLE; ?>" onclick="return sendEmails()" disabled>Submit</button> </form> This is my php code in index.php: <?php $conn = new mysqli($servername, $username, $password, $db); $sql = "SELECT sender_id, sender_email, receiver_emails, receiver_ids, file_name from filedrop_logs"; $result = mysqli_query($conn, $sql); if ($result) { echo "<div class='outputDiv'>"; echo "<table id='sharedOthers'>"; echo "<thead><tr class='headings'>"; echo "<th class='files'>Files</th>"; echo "<th class='users'>Users</th>"; echo "</tr></thead>"; while ($row = mysqli_fetch_assoc($result)) { $receiverEmails = $row['receiver_emails']; $fileName = $row['file_name']; echo "<tbody id='bodyOthers'>"; echo "<tr id='rowOthers'>"; echo "<td>$fileName<br>"; $objects = getListofObjects('FileDrop'); foreach ($objects as $object) { $file = $object['Key']; $splits = explode('/', $file); if (end($splits) !== '') { $presignedUrl = getPresignedUrlForPrivateFile($object['Key'], '+20 minutes'); $link = '<a href="'.$presignedUrl.'">Download</a>'; echo $link; } } echo " <a href=''>Delete</a></td>"; echo "<td>$receiverEmails</td>"; echo "</tr></tbody>"; } echo "</table></div>"; } ?> <?php //the if statement below doesn't execute if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES["fileName"])) { $fileName = basename($_FILES["fileName"]["name"]); $error = $_FILES["fileName"]["error"]; $tmpName = $_FILES["fileName"]["tmp_name"]; if (isset(fileName) && $fileName != '' && $tmpName != '' && sys_get_temp_dir()) { $separator = DIRECTORY_SEPARATOR; $newDir = sys_get_temp_dir() . $separator . "FileDrop" . microtime(true); if (!file_exists($newDir)) { mkdir($newDir, 0777, true); // creates temp FileDrop directory $tempFilePath = $newDir . $separator . $fileName; // creates temp file inside FileDrop directory if (move_uploaded_file($tmpName, $tempFilePath)) { // moves file to tmp folder $s3FileName = "FileDrop" . substr($newDir, 4) . $separator . $fileName; $result = putFileToS3($s3FileName, $tempFilePath, 'public-read'); deleteDir($newDir); } } } } ?> This is my js code in case you want to see it: function sendEmails() { var fileData = $('#userFile').prop('files')[0]; var formData = new FormData(); formData.append('tags', JSON.stringify(tags)); formData.append('fileName', fileData); $.ajax({ type: "POST", url: "../FileDrop/dbSystem.php", processData: false, contentType: false, data: formData, success: function(result) { result = JSON.parse(result); if (result.validity === "valid emails") { location.reload(); resetInputs(); //IMPORTANT $(".outputDiv").show(); } else { var tagsBrackets = result.emails.toString().replace(/[\[\]']+/g,''); var tagsQuotes = tagsBrackets.replace(/['"]+/g, ''); var tagsInvalid = tagsQuotes.replace(/,/g, ", "); $('#alertModal').modal({show:true}); document.getElementById('invalid').textContent = tagsInvalid; } } }); return false; } I've been stuck on this for so long, so I'd really appreciate the help!!
-
I'm trying to test error conditions for file uploads, but when a file intentionally fails, I'm not even sure how to access the code. Here's a snippet: $file_arr = $_FILES[ 'digifile' ]; $fn = $file_arr[ 'name' ]; $temp = $file_arr[ 'tmp_name' ]; $error = $file_arr[ 'error' ]; error_log( 'the error is: ' . $error ); // ** SEE NOTE BELOW if ( $error > 0 ) { $ui->upload_message = "Error during file upload. "; // try to switch the error to show a relevant message } else { if ( move_uploaded_file( $temp, $full_path ) ) // something } My test for now is simple... test a small image file, it's fine. But I have set my max file size to 16MB in php.ini, and I'm intentionally uploading a 19MB file. What is the expected result? Well I can tell you that the result is this code IS NOT REACHED AT ALL! How is that possible? error_log() doesn't get reached. I don't really know what happens when there is an error, but I'd like to show a customer a relevant message if this happened in the real world. There's no boolean result of move_uploaded_file, either, since like I said THE CODE ISN'T REACHED. I really don't understand. Any help is appreciated.
- 4 replies
-
- php
- move_uploaded_file
-
(and 1 more)
Tagged with:
-
Hello I have written the following code to update Images that are stored in Files. The code is working perfectly when I am using it to Upload images, but when I use it to Update Images, it is giving me 'Invalid file' error message. It is getting problem in the first 'if' statement and directly going to its else statement message 'Invalid File'. Kindly check it and guide me. Thanks <?php var_dump($_REQUEST); session_start(); error_reporting(E_PARSE); if (isset ($_SESSION['username']) ) { //echo "<div id='nav'"; echo "<ul><hr> <li><a href='insert_product.php' >Add Product | </a></li> <li><a href='add_category.php'> Add Category </a></li> <li><a href='sub_categories.php'> Add Sub-Category </a></li> <li><a href = 'view_products.php' >View All Products</a> </li> <li><a href = 'all_categories.php' >View All Categories</a> </li> <li><a href='view_all_sub_categories.php'>View All Sub Categories</a></li> </ul></hr>"; include 'connect.php'; $category_id= $_GET['category_id']; $query= "select * from category where category_id= $category_id"; $query_run= mysql_query($query); $fetch= mysql_fetch_array($query_run); $name= $fetch['name']; echo " <form action='update_category.php?category_id=$category_id' method='POST' > <table border=1> <tr><td> Category Name:</td><td><input type='text' name='category' value='$name' /> </td></tr> <tr><td> Image1:</td><td> <input type='file' name= 'image' > </td></tr> <tr><td> <input type='submit' value='Update' /> </td></tr> </form> </table> "; echo "<form action='delete_category.php?category_id=$category_id' method='POST'> <input type='submit' value='Delete'> </form> "; if (isset($_POST['category']) ) { $category_name = $_POST['category']; $query_update="UPDATE category SET name = '$category_name' WHERE category_id =$category_id "; if (mysql_query($query_update)) { echo "Records updated"; } else { echo mysql_error(); } /*------------------- IMAGE QUERY --------------------*/ $allowedExts = array("gif", "jpeg", "jpg", "png"); $extension = end(explode(".", $_FILES["image"]["name"])); if ((($_FILES["image"]["type"] == "image/gif") || ($_FILES["image"]["type"] == "image/jpeg") || ($_FILES["image"]["type"] == "image/jpg") || ($_FILES["image"]["type"] == "image/pjpeg") || ($_FILES["image"]["type"] == "image/x-png") || ($_FILES["image"]["type"] == "image/png")) //&& ($_FILES["file"]["size"] < 200000) && in_array($extension, $allowedExts)) { if ($_FILES["image"]["error"] > 0) { echo "Return Code: " . $_FILES["image"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["image"]["name"] . "<br>"; echo "Type: " . $_FILES["image"]["type"] . "<br>"; echo "Size: " . ($_FILES["image"]["size"] / 200000) . " kB<br>"; $image_name= $_FILES["image"]["name"]; $random_name= rand().$_FILES["image"]["name"]; $path= move_uploaded_file($_FILES["image"]["tmp_name"], "upload/categories/" . $random_name); $folder="upload/categories/" .$random_name; echo "Stored in: "."upload/categories/". $random_name; echo $sql= "update category_images set name='$image_name' , location='$folder' where category_id= $category_id "; $result = mysql_query($sql); if ($result) { echo "successfull"; } else { echo mysql_error(); } } } else { echo "Invalid file". $_FILES["image"]["error"]; } /*----------------- IMAGE QUERY END ------------------*/ } /* echo $sql= "update category_images set name='$image_name' , location='$folder' where category_id= $category_id "; */ } else { echo "You Must need to Log in to Visit this Page"; } ?>
- 3 replies
-
- php
- uploading images
-
(and 2 more)
Tagged with:
-
I am new to this so this is probably a stupid question but, how do I access [userfile1] and [userfile2] in the following code to write a foreach statement that runs on each of those arrays in $_FILES. For context: these files populate $_FILES via an html form with two browse buttons - one for each file to upload - and one submit button. The following output was produced with <pre><?php print_r($_FILES); ?> </pre> . Any help is appreciated. Thanks. Array ( [userfile1] => Array ( [name] => Hay-Bales-LeoKenney.jpg [type] => image/jpeg [tmp_name] => C:\wamp\tmp\php441.tmp [error] => 0 => 356369 ) [userfile2] => Array ( [name] => LK-Holding on.jpg [type] => image/jpeg [tmp_name] => C:\wamp\tmp\php442.tm [error] => 0 => 312401 ) )