hey guys, brand new here.
before i go on let me say that,
i searched the forums for my problem but the latest thread with any remotely similar issues was in like 2007.
i'm making an image uploader for my family on my server, so we have a nice place to look through all our pictures on the fly.
i've snooped around a bit and slapped this little thing together. so far it takes an image, renames it with a randomly generated number, and uploads it to the folder of my choosing. i've included my problems and comments as comments in the code:
another concern is when the page is refreshed, it asks to resend data, how can i eliminate this request? i would just want the uploader to be reset, otherwise a duplicate is sent everytime the page is reloaded or the back button is hit once leaving the page. i read that i can use a javascript redirect, but when i try implementing the couple i've found it doesn't work or the page doesn't even load. i also read that 'session' can be used, but i'm trying to figure that out.Code:<?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 2000000)) /* so from the above you see it only takes jpeg or gifs, but i want it to take all image types, and rather than listing them all i want to use this clever bit i found in another example, $image_size = getimagesize($_FILES['file']['tmp_name']); if ($image_size==FALSE) echo "that's not an image."; else { this would save a lot of lines and make it more dynamic, it would just check if the file has an image size, if not it would just kick it out. but i'm having trouble implementing it. i figure i have to define $image first right? how would i define $image as the image? i would put the ending brace for the else statement at the bottom just before the php close tag.(not sure if that's right either, but it doesn't work :/) anyway it goes, */ { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; /*btw i set the image size limit at 2mb for now, but if i change this echo to something like "your image is over 2MB reduce it and try again!" it doesn't display the message. i have no idea why not... */ } else { //here the image name splits from the extension and is renamed, then reconnected. function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } $ext = findexts ($_FILES['file']['name']) ; $ran = rand () ; $ran2 = $ran."."; $target = "random/"; $target = $target . $ran2.$ext; echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("random/" . $ran2.$ext)) { echo $ran2.$ext . " already exists. "; /*also i know this is retarded, but i'm still trying to figure out how to put a while loop here so it renames the image again in case of a same name. i figure it's something like: while(file_exists("random/" . $ran2.$ext)) { $ran = rand () ; $ran2 = $ran."."; $target = "random/"; $target = $target . $ran2.$ext; } but i'm not entirely sure if that will work. kind of hard to test it when a random number is generated everytime lol */ } else { move_uploaded_file($_FILES["file"]["tmp_name"], $target); echo "Stored in: " . "mything.com/random/" .$ran2.$ext; } } } else { echo "Invalid file"; /*this echo comes up even on page load, before an image is even chosen or uploaded. i'm sure it would cause mass confusion but i'm still trying to figure out how to fix this. maybe i should just change it to 'ready for image upload' since i have that other error catch in place. */ } ?>
any ideas? i know it's a big mess, but that's why i'm looking for some guidance, my php is lacking x.x



Reply With Quote
in case anyone is interested i've updated the code a bit. i made the pictures name count up from 0 instead of just giving a random number, so the while loop works. i'm still trying to figure out how to use
Bookmarks