PHP - Html/php/javascript Help - Image Size And Extension Form Validation
I am working on a PHP project that will allow a user to upload a handful of images, and then submit text to go along with it.
To simplify the uploading process of a file, I restricted the type of file that can be uploaded (jpg) and the size (2.5mb). The problem is, the form is calling to a PHP script, and I have error logging built in to alert the user of why a file didn't upload, but since the PHP script is just being called, no errors are being displayed on the page. I guess unless I do a refresh of a few seconds to view any errors on the page.... What I would like to do is show a pop-up/error message notifying the user that the file type or size is incorrect and needs to be adjusted. Now part of the problem is, the file needs to be uploaded to the server first, and then everything can be checked, so I am really getting confused. If someone can point me in the right direction on what I could use to accomplish this, it would be greatly appreciated. Thanks -beemer Similar TutorialsLet's say I have a form with 5 fields to fill out Is it better practice to use Javascript to ensure that all fields are non-empty, a certain length, contain certain characters, etc. Or should the back end PHP side of things handle this, then return an error code to be printed out, etc Thanks! Hi. So I've coded many a file upload form and never had any trouble. Until now. For some reason when I try to upload MP3 files, I'll hit submit and the page just reloads. Once it does so, the file upload field is once again blank. What's odd is that images and documents upload just fine. It's only audio tracks that cause the page to reload and clear. My php.ini max file upload size is at this point set to 100M, and the files I'm trying to upload are under 10M. Any ideas? Here's the form code, just to post it-- pretty basic. I'm at a loss here.... Any ideas would be appreciated. Thanks very much. <form action="test.php" enctype="multipart/form-data" method="POST"/> <input type = "file" name = "audio" /> <p><input type ="submit" name="submit" value="do it."/><p> </form> I'm a major JS noob and this is driving me nuts. I'm trying to use this in a form:
http://reactiveraven...trapValidation/
And using the examples, can't get anything to work. Only my browser's non-js HTML 5 form validation. Anybody have a simple that incorporates both HTML and JS? The examples only show the html.
Hi there. My problem is quite simple, but carries lots of questions of why the hell is this happening. I'm new at PHP but I'm familiar with the syntax, since It's been 3 years since I code in C, 1 in Java and half year in JS (with HTML and CSS of course). I was creating a page where I could validate a HTML form using PHP script, it was going everything allright, until I couldn't use the data that was going to the $_POST array. I tested it on some other file and It was like the server just didn't allowed me to do it so. I searched around on internet and I found the print_r() function, which shows the data as an associative array in $_POST, like this: array(KeyName => ValueName). Check the code and please, help me find why is this happening. I'm brazillian and all I know about english came from netflix, so i'm sorry if I wrote anything wrong. Thanks S&Z
<!DOCTYPE html> This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=342898.0 could someone help in how to resize a uploaded image size, say if someone is uploading a 1mb photo to my server, i wish for it to become 100kb and also resize its width and height? here is my code Code: [Select] //////////////////////////////////////uploader else if($action=="uploader") { echo "Upload your picture and copy the link <br/>after uploading to user it at gallery.<br/><br/>"; echo "<form method=\"post\" enctype=\"multipart/form-data\" action=\"index.php?action=uploaded&sid=$sid\">"; echo "Choose Pictu <br />"; echo "<input name=\"uploaded\" type=\"file\" /><br /><br />"; echo "<input type=\"submit\" value=\"Upload\" />"; echo "</form><br/>"; echo "<p align=\"center\">"; echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>"; echo "</p>"; } //////////////////////////////////////uploader else if($action=="uploaded") { $blacklist = array(".php", ".php.jpg", ".php.jpeg", ".php.gif", ".php.png", ".phtml", ".php3", ".php4"); foreach ($blacklist as $item) { if(preg_match("/$item\$/i", $_FILES['uploaded']['name'])) { echo "<p align=\"center\">"; echo "Oops sorry we do not allow those files.<br/>"; echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>"; echo "</p>"; exit; } } $target = "../images/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; if (file_exists("../images/" . $_FILES["uploaded"]["name"])) { echo "<p align=\"center\">"; echo $_FILES["file"]["name"] . "Oops file name already exists<br/> kindly rename your picture and upload again. <br/>"; echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>"; echo "</p>"; }else{ //This is our size condition if ($uploaded_size > 25600){ echo "Your file is too large. We have a 25kb limit.<br/>"; $ok=0; } $types = array('image/jpeg', 'image/gif', 'image/png'); if (in_array($_FILES['uploaded']['type'], $types)) { // file is okay continue } else { $ok=0; } //Here we check that $ok was not set to 0 by an error if ($ok==0){ echo "<p align=\"center\">"; Echo "Sorry your file was not uploaded.<br/> It may be the wrong filetype. <br/>We only allow JPG, GIF, and PNG filetypes.<br/>"; echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>"; echo "</p>"; } //If everything is ok we try to upload it else{ if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){ echo "<p align=\"center\">"; echo "The file ". basename( $_FILES['uploadedfile']['name']). " Picture uploaded successfully.<br/><br/><b>$target <br/>"; echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>"; echo "</p>"; } else{ echo "<p align=\"center\">"; echo "Sorry, there was a problem uploading your file.<br/>"; echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>"; echo "</p>"; } } } } Hi first post here and very new to php. I built a form so that a data entry perosn can "build" new pages for our companies website. I pretty much have the basics of the form and the action page coded correctly, however my issue is that I need this final page where the form inputs are posted too to be an html document. I read some about the str_replace() function, but I'm not understanding how I could use that to replace the file extension of a page. Maybe I'm going about this all wrong. I also read that one can edit the htaccess folder to allow an html file to run php script. Not sure which way would be the best. One thing to mention though, is this application would be run locally to our back up files which is then scheduled to be uploaded to the remote server very night. Any help or suggestions would be greatly appreciated. I have a website that I did for my uncle and now, the requirements are going beyond my html knowledge and into the realm of server scripting. My issue is that I need to loop through all the images in a given folder and subfolders and preload them into the html so that the user can view them within a slide show. There are currently 6 subfolders with over 20 pics in each and there are more pics to come and different folders to add. I have a media page with the first picture within each category (subfolder) displayed as an eye catcher to the category. once the user clicks on the image, the slide show will start in a popup. The problem is that with js, I have to list out every image since it cannot look through a directory listing. I would like to simply call a server side script that will create an array of the images and preload them so that when the slide show is ran, the appropriate images will show. I am found numerous scripts for preloading images, written in php, but I am unsure how to implement them and keep a html extension for my pages. As the site is written entirely in html and javascript, I would like to keep the html extentions if possible. As this is my first site, I am sure that server side scripting will become a bigger role, but baby steps are what I need right now. In addition, if anyone has a good site for learning the server scripting world, I would greatly appreciate the reference. Hi folks, just upgrading one of my sites with a new script which i thought i had working, well it did at least on my local server. Its an image upload script. It fails at the get image extension part, which when echoed out is blank. Which is why its failing. Can anyone see what i have missed? if($_POST){ $image =$_FILES["image"]["name"]; $img = $_FILES['image']['tmp_name']; $size = filesize($_FILES["image"]["tmp_name"]); $maxsize= 400; if(isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH']>409600){ echo 'Upload FAILED, file is too large !'; exit(); } if ($size > $maxsize*1024){ echo "File too big"; exit(); } else { if(file_exists($existingimage)){ unlink($existingimage); } function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $extension = getExtension($image); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo "The extension is:" . $extension ." "; echo'Unknown Image extension'; exit(); } Ive ended the script here at the part where it fails to reduce the amount of uneeded code to sift through. It fails no matter what type of image is used to upload. Hi everyone, Sorry not sure if this is a php or html problem. Im using php and a html form to upload images to my site, i have made it so that jpg, jpeg, gif and png images can be uploaded. The problem im having is displaying the image. Code: [Select] <img src="images/<?php echo $name ?>.jpg" /> That works fine if a jpg was uploaded, but what if a png or a gif was uploaded? The $name is going to be unique, there will not be more than one image with the same name, so what do i have to do to display the image regardless of what the extension is? Thanks Help me I am getting Notice: Undefined index: extension good day dear community, this is a big issue. I have to decide: between native PHP DOM Extension or of simple DOM html parser well i want to parse the site he http://buergerstiftungen.de/cps/rde/xchg/SID-A7DCD0D1-702CE0FA/buergerstiftungen/hs.xsl/db.htm http://buergerstiftungen.de/cps/rde/xchg/SID-A7DCD0D1-702CE0FA/buergerstiftungen/hs.xsl/db.htm I will suggest to use the native PHP "DOM" Extension instead of "simple html parser", since it will be much faster and easier What do you think about this one here...: Code: [Select] $doc = new DOMDocument @$doc->loadHTMLFile('...URL....'); // Using the @ operator to hide parse errors $contents = $doc->getElementById('content')->nodeValue; // Text contents of #content look forward to hear from you best regards db1 The script for creating a new file name for the image:
$validextensions = array("jpeg", "jpg", "png"); //Extensions which are allowed $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.) $file_extension = end($ext); //store extensions in the variable $new_image_name = md5(uniqid()) . "." . $ext[count($ext) - 1]; $target_path = $target_path . $new_image_name;//set the target path with a new name of imageThe script creates a new file like: f6c9b8d9db05366c3504210cded9ddb2.jpgand moves the file to the "uploads" folder. And then the script also creates a thumbnail with the same file name and moves the file to the "thumbs" folder. The issue I am having is that the same ID code could happen again for a different image in the database, thus I would be calling a different original sized image than the thumbnail image. My question is: How to avoid this issue of the same ID code has happened again for a different file. What is the proper way to reference the anchor tag of the thumbnail image to its actual original sized image? With the script I have the thumbnail image would be coming from the "thumbs" folder and the anchor tag would get referenced to the "uploads" folder to get the original sized image. Edited by glassfish, 12 October 2014 - 05:51 AM. Is there a way I can get the .jpg file in kilobytes from a remote url and check it to make sure it does not exceed 40 kilobytes before it saves the remote url into my database? Hey My php function keeps returning an error on a valid image! I get this error: Quote could not make seekable I use this line to get the image size but i get the above error for it:$picturearray = getimagesize($Image); Url being tested : Code: [Select] http://www.fileden.com/files/2010/12/22/3043070/Sidroc.jpg The url is correct and the image does exist - does any one know what can cause this issue ? I use a simple php code (taken from php.net example) to resize an image Code: [Select] list($width, $height) = getimagesize($filename); $new_height = $height / $width * 400; $image_p = imagecreatetruecolor(400, $new_height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, 400, $new_height, $width, $height); imagejpeg($image_p, "new.jpg"); Here, we create a blank image and merge it with the resized image. I wonder if it is the simplest way to resize and save an image. Is it really necessary to create a blank background image? Hi, I'm developing a code that uploads images. I need to verify if the image have the minimum size of 100*100 pixels before the upload. How can I do this? Best Regards, Sorry for my bad english. hello anyone can help me to reduce image file size(reduce quality of images) using php ? Hey there so I'm using the below code to upload images/files to my server via ftp. The issue I've found is that anything bigger than 200KB either stalls out and partially uploads or doesn't upload at all. I've set my php.ini file to have upload_max_filesize = 1000M and post_max_size = 1000M. And still anything bigger than 200KB won't upload right. Any help would be appreciated. I'm using php version 5.2.14 fyi. Thanks so much. ---CODE--- <?if(!isset($_POST["submit"])){?> <!-- THE FORM ACTION IS ONTO ITSELF --> <form action="index.php" method="POST" enctype="multipart/form-data"> <input name="userfile" type="file" size="40"> <input type="submit" name="submit" value="Upload image" /> </form> <? } else { set_time_limit(30000);//i noticed that this had to be pretty big otherwise your upload was likely to stall out. $paths='/yourfilepath/yourfilepath/'; $filep=$_FILES['userfile']['tmp_name']; $ftp_server='ftp.yourdomain.com'; $ftp_user_name='your_user_name'; $ftp_user_pass='your_password'; $name=$_FILES['userfile']['name']; $conn_id = ftp_connect($ftp_server); $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); if ((!$conn_id) || (!$login_result)) { echo "FTP connection has encountered an error!"; echo "Attempted to connect to $ftp_server for user $ftp_user_name...."; exit; } else { echo ""; } $upload = ftp_put($conn_id, $paths.'/'.$name, $filep, FTP_BINARY); if (!$upload) { echo "FTP upload has encountered an error!"; } else { echo "Uploaded file with name $name Succsessfully "; } ftp_close($conn_id); } ?> |