PHP - Issue With Saving Uploaded Image Twice
Hello. I'm using an Amazon S3 class to uploaded to S3. I have 2 upload boxes - The first uploads once and the second needs to upload twice - 1 full size, 1 thumb.
The issue i'm having is that the 2nd image (the thumb) seems to be failing, although if I don't save the first full sized image I am able to upload the thumb. So I think the issue is with using the temp file twice? This is my code: //retreive post variables $fileName = $randomString . "_" . $_FILES['theFile']['name']; $fileTempName = $_FILES['theFile']['tmp_name']; $fileName2 = $randomString . "_" . $_FILES['theFile2']['name']; $fileTempName2 = $_FILES['theFile2']['tmp_name']; //move the file if ($s3->putObjectFile($fileTempName, "containerhere", $fileName, S3::ACL_PUBLIC_READ)) { echo "<strong>Uploaded Image</strong>"; }else{ echo "<strong>Something went wrong while uploading your file... sorry.</strong>"; } //move the file if ($s3->putObjectFile($fileTempName2, "containerhere", $fileName2, S3::ACL_PUBLIC_READ)) { echo "<strong>Uploaded Image</strong>"; }else{ echo "<strong>Something went wrong while uploading your file... sorry.</strong>"; } include('simpleImage.php'); $image = new SimpleImage(); $image->load($_FILES['theFile2']['tmp_name']); $image->resizeToWidth(100); $image->save($_FILES['theFile2']['tmp_name']); $fileName3 = $randomString . "_" . $_FILES['theFile2']['name']; $fileTempName3 = $_FILES['theFile2']['tmp_name']; //move the file if ($s3->putObjectFile($fileTempName3, "containerhere", "thumbs/" . $fileName3, S3::ACL_PUBLIC_READ)) { echo "<strong>Uploaded Image</strong>"; }else{ echo "<strong>Something went wrong while uploading your file... sorry.</strong>"; } Can anyone offer any advice? Thanks. Similar TutorialsHi I'm really new to php and could really do with some help. I am using the following script http://test.dpetroff.ru/jquery.iviewer.test/test/ which basically allows me to position an image within a DIV area My question is, how can I get PHP to save just the image within the DIV area, so it's almost acting like a crop tool I've tried a few things but so far not managed to get anything working Hi. I have a script here that will let users upload an image to my website but I just can't figure out how to save the uploaded image as "upload/logo.png" so that it will replace the already existing "upload/logo.png". Help would be greatly appreciated. Code: [Select] <html> <body> <form action="" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> <?php if(isset($_POST['submit']) && !empty($_FILES["file"]["name"])) { $timestamp = time(); $target = "upload/"; $target = $target . basename($_FILES['uploaded']['name']) ; $ok=1; $allowed_types = array("image/gif","image/jpeg","image/pjpeg","image/png","image/bmp"); $allowed_extensions = array("gif","png","jpg","bmp"); if ($_FILES['file']['size'] > 350000) { $max_size = round(350000 / 1024); echo "Your file is too large. Maximum $max_size Kb is allowed. <br>"; $ok=0; } if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; $ok=0; } else { $path_parts = pathinfo(strtolower($_FILES["file"]["name"])); if(in_array($_FILES["file"]["type"],$allowed_types) && in_array($path_parts["extension"],$allowed_extensions)){ $filename = $timestamp."-".$_FILES["file"]["name"]; echo "Name: " . $filename . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; $path_parts = pathinfo($_FILES["file"]["name"]); echo "Extension: " . $path_parts["extension"] . "<br />"; echo "Size: " . round($_FILES["file"]["size"] / 1024) . " Kb<br />"; //echo "Stored in: " . $_FILES["file"]["tmp_name"]. " <br />"; } else { echo "Type " . $_FILES["file"]["type"] . " with extension " . $path_parts["extension"] . " not allowed <br />"; $ok=0; } } if($ok == 1){ @move_uploaded_file($_FILES["file"]["tmp_name"], $target . $filename); $file_location = $target . $filename; if(file_exists($file_location)){ echo "Uploaded to <a href='$file_location'>$filename</a> <br />"; } else { echo "There was a problem saving the file. <br />"; } } } else { echo "Select your file to upload."; } ?> Thanks! Hello dear, I'm willing to use thumbnails into my website which is mainly like websites directory. I've been thinking to save url thumbnails into certain directory ! If i've index.php with the following image code (this would show thumbnail image of the website ANY_SITE.COM Code: [Select] <img src='http://thumbnails_provider.com/url=ANY_SITE.COM'/> then how to save the generated image into certain directory like my_site.com/thumbnails i've been thinking about Using cURL but i don't know how to apply it any help ~thanks Hi Guys, Using this code i am able to overlay a logo onto an image: <?php // Load the image where the logo will be embeded into $image = imagecreatefromjpeg("box.jpg"); // Load the logo image $logoImage = imagecreatefrompng("logo.png"); imagealphablending($logoImage, true); // Get dimensions $imageWidth=imagesx($image); $imageHeight=imagesy($image); $logoWidth=imagesx($logoImage); $logoHeight=imagesy($logoImage); // Paste the logo imagecopy( // source $image, // destination $logoImage, // destination x and y $imageWidth-$logoWidth, $imageHeight-$logoHeight, // source x and y 0, 0, // width and height of the area of the source to copy $logoWidth, $logoHeight); // Set type of image and send the output //header("Content-type: image/png"); //imagePng($image); imagejpeg($logoImage, "imageWithLogo.jpg", 100); // Release memory imageDestroy($image); imageDestroy($imageLogo); ?> this part: //header("Content-type: image/png"); //imagePng($image); shows me the complete image with the logo ontop, what i'm having trouble with is saving the image. any help would be appreciated thanks guys Graham I'm beginning with simple codes to understand how things work and work my way up with what I need. My upload form looks like this: <form name="upload about" action="display.php" enctype="multipart/form-data" method="post"> Header: <input type="text" name="header" /> Body: <input type="text" name="body" /> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> Image: <input type="file" name="pic" /> <input type="submit" /> </form> And my display page is just as simple. <?php echo $_POST["header"]; ?> <?php echo $_POST["body"]; ?> <?php echo $_FILES['pic']['name']; ?> The problem is, whenever I submit the data, everything is fine, except the image does not display and just posts the filename of the image I uploaded. According to my research setting the enctype to multipart/formdata should display the image, but it does not. Can anyone tell me what's wrong? Hi everyone, I have a script below, which uplads an image, however, the image name always starts with a capital letter, I want all letters to be small, how to adjust this please, thank you
$target_dir = ""; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $target_file = "$get_current_user.jpg"; $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); // Check if image file is a actual image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo ""; /// was File is an image $uploadOk = 1; } else { echo "File is not an image<br>"; $uploadOk = 0; } } // Check file size if ($_FILES["fileToUpload"]["size"] > 10000000) { echo "Sorry, this image is too large, please resize using Paint<br>"; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo "Only JPG, JPEG, PNG & GIF files are allowed<br>"; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo "There was an error<br>"; } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "Uploaded successfully (You may need to clear Cache to see the new image)"; mysql_query("UPDATE user SET user_image = 'https://.../images/users/$get_current_user.jpg' WHERE user_name = '$get_current_user' "); } else { echo ""; /// was Select a suitable image, file not uploaded yet } }
Hello I have uploaded images from a server onto a website, they run through a mysql database (database holds the image file names). The problem I am having is resizing the images. Can anyone help? Thank you in advance GWG Not sure if this is the right place to post this. I have PHP form that I use to upload a document, PDF or Word Doc, I would also like the form to create a thumbnail of the document when it is uploaded, is this possible? Hi, I have a script that uploads an image to a directory and then saves the fill path to a field in a table for later use. The only problem is people are uploading huge images and then when I produce a catalogue of my listings it takes forever to load because the images are so big. What I am after is an add in script to create an ADDITIONAL image 100 x 75px, I don't really want to change my upload script. Any ideas? Thanks in advace. Here is what I have: Code: [Select] <?php $idir = "../fleet/"; // Path To Images Directory if (isset ($_FILES['fupload'])){ //upload the image to tmp directory $url = $_FILES['fupload']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['fupload']['type'] == "image/jpg" || $_FILES['fupload']['type'] == "image/jpeg" || $_FILES['fupload']['type'] == "image/pjpeg") { $file_ext = strrchr($_FILES['fupload']['name'], '$account.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['fupload']['tmp_name'], "$idir" . $_FILES['fupload']['name']); // Move Image From Temporary Location To Perm } } $fleetimage1 = mysql_real_escape_string("$idir" . $_FILES['fupload']['name']); //then insert sql code below... ?> Hey. I'm just wondering what would be the most logical or most efficient way to save an image resource that has been constructed on say the www. to a subdirectory or on another webserver? For eg The script on www.example.com constructs an image using the GD library from image1.png and image2.png. It has now got the final image resource for what's been constructed. I now want to save this image onto images.example.com How would you suggest? Hey guys! I have the following php code that grabs variables (and the browsed image) from Flash. //FLASH VARIABLES $Name = $_POST['Name']; $itemNumber = $_POST['itemNumber']; $filename = $_FILES['Filedata']['name']; $filetmpname = $_FILES['Filedata']['tmp_name']; $fileType = $_FILES["Filedata"]["type"]; $fileSizeMB = ($_FILES["Filedata"]["size"] / 1024 / 1000); list($filename, $extension) = explode('.', basename($_FILES['Filedata']['name'])); $filename = $Name; $target = $filename . $itemNumber . "." . $extension; // Place file on server, into the images folder move_uploaded_file($_FILES['Filedata']['tmp_name'], "images/".$target); This works perfect, but what I want to change is the width and height of the uploaded image. Any ideas/suggestions on how this could be done? Thanks in advance!! Cheers! OK, When the user fills the info in the form out it goes in the DB fine. I can then array them on the "showroom page" fine. When they upload a picture it goes into the /images/ folder fine. Problem is... On each array on the showroom page I need the image they uploaded to be displayed. Cant work it out. Help would be GREAT!!!!! hi to all.Im currently displaying the images from my upload directory but the image does not display.please help thanks Hi Basically I've built a CMS where by my clients can upload a number of images. On the success page I want to display the images they uploaded by file name. The issue is the number of images can vary. They may upload 2 or 10 or 50 etc. So far I've come up with this: Code: [Select] // number of files $UN = 3; //I've set this to 3 for now, but this is passed from the upload page! // server directories and directory names $dir = '../properties'; $images = glob($dir.'/*.{jpg}', GLOB_BRACE); //formats to look for $num_of_files = $UN; //number of images to display from number of uploaded files foreach($images as $image) { $num_of_files--; $newest_mtime = 0; $image = 'BROKEN'; if ($handle = @opendir($dir)) { while (false !== ($file = readdir($handle))) { if (($file != '.') && ($file != '..')) { $mtime = filemtime("$dir/$file"); if ($mtime > $newest_mtime) { $newest_mtime = $mtime; $image = "$file"; } } } } if($num_of_files > -1) //this made me laugh when I wrote it echo $trimmed = ltrim($image, "../properties").'<br />'; //display images else break; } Without this piece of code: Code: [Select] $newest_mtime = 0; $image = 'BROKEN'; if ($handle = @opendir($dir)) { while (false !== ($file = readdir($handle))) { if (($file != '.') && ($file != '..')) { $mtime = filemtime("$dir/$file"); if ($mtime > $newest_mtime) { $newest_mtime = $mtime; $image = "$file"; } } } } It shows the first 3 files alphabetically. I want to view the last number of images added. With the above code it simply shows the last image added 3 times! So I need to get the time each image was added and then order by the newest added and limit to the number of images uploaded. Any suggestions please? Kindest regards Glynn Hi All, This is my first post on here. I'd really appreciate any help with this, it's driving me mad. I'm trying to create a form to save a image into a mySQL database. I have a website hosted by UK2.net which has a mysql db with a table called gallery(name (varchar 30), size (int), type varchar(30), thePic(mediumBlob)). I have a form with this: Code: [Select] <td><p><label>Pic: </label></td><td><input name="userfile" type="file" id="userfile" /></td></p> Which when submitted actions addImage.php. The code for this looks like this: Code: [Select] <?php $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } $con = mysql_connect("localhost", "userName", "password") or die(mysql_error()); mysql_select_db("dbName", $con) or die(mysql_error()); $query = "INSERT INTO gallery (name, size, type, thePic) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; if (!mysql_query($query,$con)) { die('Error: ' . mysql_error()); } echo "<br>File $fileName uploaded<br>"; ?> I get the following error message: Quote Warning: fopen() [function.fopen]: Filename cannot be empty in /home/hiddenje/public_html/addImage.php on line 13 Does anyone know what this is about? I've been on out friend google and a lot of people seem to be pointing to permissions but I can't seem to apply it to my scenario and just can't get it to work. Im a developer by trade, but this is my first step into the...interesting world of PHP and mySQL. Again, I'd appreciate any help with this. I'd love someone to talk me through exactly what I'm missing or doing wrong. Thanks in advance. I need help making the uploaded image file name, that's chosen to be uploaded, be displayed on the html page with the path /upload/ added to the beginning of the displayed file name like so: ../upload/test.png Any help/improvements will be appreciated. <html> <head> <title>PHP Test</title> </head> <body> <?php if ($form_submitted == 'yes') { $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES["file"]["name"]); $extension = strtolower( end($temp) ); if ( $_FILES["file"]["size"] < 200000 && in_array($extension, $allowedExts) ) { if ($_FILES["file"]["error"]!= 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; } else { $length = 20; move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename ); $file_location = '<a href="http://../upload/' . $newfilename . '">' . $newfilename . '</a>'; } } else { echo "Invalid upload file"; } ?> <label for="file">Filename:</label> <input type="file" name="file" id="file"> </body> </html> I am working on a simple gallery. I want to have an option to download full scale image without users having to right click and "save as". Using this code you get a image preview: <a href="pictures/img-(1).jpg"><img src="thumbnails/img-(1).jpg"></a> How can I modify it so when user clicks on thumbnail of image 1 it will prompt him to save the image instead of viewing it. Thanks 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>"; } } } } Hello, currently, I got myself a site where you have your own profile and your own 'profile image'. I allow people to upload their own image, and checking my folder, it indeed uploads it correctly. Now the issue starts when you try to delete your image, again - when deleted the image is deleted in the folder.. However, when then uploading a NEW image, it simply displays the old one (While the new one is actually in the folder!?) I figured that if you would close the website, and then load it again - it works correctly (figured this means the old image is still loaded in your temp files or something?) Now I was wondering if you could actually somehow *force* the user to re-download this image when you upload a new one. Thanks for your time! i have a code that generates a random code and displays it as an image. But the problem the image can be displayed only if it is called before any other output. How do i run it so that it can be displayed after any output.. Code: [Select] <? //Security Word for the form $codelenght = 10; while($newcode_length < $codelenght) { $x=1; $y=3; $part = rand($x,$y); if($part==1){$a=48;$b=57;} // Numbers if($part==2){$a=65;$b=90;} // UpperCase if($part==3){$a=97;$b=122;} // LowerCase $code_part=chr(rand($a,$b)); $newcode_length = $newcode_length + 1; $newcode = $newcode.$code_part; } header('Content-Type: image/png'); $im = imagecreatetruecolor(200, 40); $white = imagecolorallocate($im, 255, 255, 255); $grey = imagecolorallocate($im, 128, 128, 128); $blue = imagecolorallocate($im, 0, 0, 238); $black = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 1, 1, 200, 40, $white); $font = '/home/vhosts/master_files/advertising/fonts/arialbd.ttf'; imagettftext($im, 12, 0, 10, 18, $blue, $font, $newcode); imagepng($im); imagedestroy($im); ?> |