Ninjakreborn Posted November 1, 2006 Share Posted November 1, 2006 I have 30 file's I allow to be uploaded at a time on one project<label for="file1">Image 1</label><input name="file1" id="file1" type="file" /><br />that is what each one looks like just with a different numberon the other page I am testing for the upload withif (isset($_POST['file1'])) {// file was uploaded} Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/ Share on other sites More sharing options...
gmwebs Posted November 1, 2006 Share Posted November 1, 2006 I am not sure I understand the question...? Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118144 Share on other sites More sharing options...
trq Posted November 1, 2006 Share Posted November 1, 2006 Read the link in my signiture. Seriously. Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118145 Share on other sites More sharing options...
Ninjakreborn Posted November 1, 2006 Author Share Posted November 1, 2006 I have a form with 31 potential file uploads. I want to test each file to see if they tried to upload a file, if they did, then I go ahead and check if the file exists, and rename it, or whatever else.I only want ot try to work with file's however they they attempted to upload. The one's they left blank I want to ignore, but that didn't work, like if it was a text field, that would check to see if it was field out, if it was then I can validate it, it's not working with file uploads.Sorry it was so unclear at first. trying to deal with 30 file uploads at once has got me feeling a little dizzy, and my head is spinning. I went from a few huge things today, over to trying to do mass file uploads. It's been a hell of a day, that's why I sound out of it, sorry about that. Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118146 Share on other sites More sharing options...
gmwebs Posted November 1, 2006 Share Posted November 1, 2006 One way is to post an array of files, and then iterate through the array with a while loop, moving and renaming each file until there aren't anymore. Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118152 Share on other sites More sharing options...
roopurt18 Posted November 1, 2006 Share Posted November 1, 2006 Anything uploaded will appear in $_FILES. Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118155 Share on other sites More sharing options...
Ninjakreborn Posted November 1, 2006 Author Share Posted November 1, 2006 How do you just test for an upload attempt.If you have a input field, it's it's just <input name="hello" id="hello" type="text" />then you can just goif (isset($_POST['hello'])) {// field was filled out.}I need to get the same type of affect with file upload's, but it doesn't work like this, how do I test for it? Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118156 Share on other sites More sharing options...
gmwebs Posted November 1, 2006 Share Posted November 1, 2006 In your form, for each upload field you have, name it like this...[code]<input name='upload_file[]' type='file' >[/code]Then in the script you are POSTing to, the files can be accessed by [code=php:0]$_FILES['0'][/code] through to [code=php:0]$_FILES['29'][/code]. Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118157 Share on other sites More sharing options...
Ninjakreborn Posted November 1, 2006 Author Share Posted November 1, 2006 I know how to access the file's specifically. I am just wondering hwo to do a test for the file.If they didn't attempt to upload one it doesn't show, if they did, then it tries to work with it.will this workif (is_uploaded_file($_FILES['file1'])) {// validate file// rename file// move file}then if it didn't then it just ignores that block of code? Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118159 Share on other sites More sharing options...
gmwebs Posted November 1, 2006 Share Posted November 1, 2006 The $_FILES array will *only* contain the files that were uploaded, so if your user only used 5 fields, then there would only be 5 files to work with in the $_FILES array (0 through to 4) so you would not need to test for their existance. Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118162 Share on other sites More sharing options...
Ninjakreborn Posted November 1, 2006 Author Share Posted November 1, 2006 The reason I am wondering about doing it the long way.I want to get solid with working on file's I don't work on them often, I am still weak at them. I want to get a lot better at individually working on seperate file's then later i will start doing it with an array. Right now I just want to 1 at a time test to see if each file was uploaded.Like thisif (they attempted to upload file1) {// validate file, rename file, move file}else {// ignore existence}if (they attempted to upload file2) {// validate file, rename file, move file}else {// ignore it}if (they attempted to upload file3 ) {// validate file, rename file, move file}else {//ignore it}and so forth for each of the 31 files. This will get me better at them individually. THen later I can go in and learn how to do them with array's to make it 4 time's faster. Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118165 Share on other sites More sharing options...
gmwebs Posted November 1, 2006 Share Posted November 1, 2006 Well... whether you upload 1 or 300 files, they are still in the $_FILES array. If you want to work through each one separately writing 100 lines of code when one while loop will do it, then be my guest ;D[code]<?php// Fetch the file array for file1$file1 = $_FILES['file1'];// Count the number of files uploadedif (count($file1) > 0) {//do your code to move and rename file1}//repeat many times over... ;)?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118169 Share on other sites More sharing options...
Ninjakreborn Posted November 1, 2006 Author Share Posted November 1, 2006 I am not explaning it right. How do I test to see if they put something in the little box where the file is.<input name="file1" type="file" />there is a blank line there, if they upload a file it fills it with the filename of there server, if not it sends it blank, I just want to check to see if they clicked upload, and picked a file, for that specific one before hitting submit. Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118172 Share on other sites More sharing options...
roopurt18 Posted November 1, 2006 Share Posted November 1, 2006 Every form field of the following type:<input type="file" name="some_name"/>creates a corresponding entry in the $_FILES array. You check if the user put something into the field by checking for the corresponding entry in $_FILES. Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118178 Share on other sites More sharing options...
gmwebs Posted November 1, 2006 Share Posted November 1, 2006 Each file upload field on your form has a name.[code]<input name='file1' type='file' /><input name='file2' type='file' /><input name='file3' type='file' /><input name='file4' type='file' /><input type="submit" name="submit" value="upload" />[/code]On your processing script, there will be a $_FILES array for each field.[code=php:0]$_FILES['file1][/code][code=php:0]$_FILES['file2][/code][code=php:0]$_FILES['file3][/code][code=php:0]$_FILES['file4][/code]You would then test each field to see if it contains any values in the array, and if it does, perform your move and rename functions.[code]<?php// Fetch the file array for file1$file1 = $_FILES['file1'];// Fetch the file array for file2$file2 = $_FILES['file2'];// Fetch the file array for file3$file3 = $_FILES['file3'];// Fetch the file array for file4$file4 = $_FILES['file4'];// Check file1if (count($file1) > 0) {//do your code to move and rename file1}// Check file2if (count($file2) > 0) {//do your code to move and rename file1}// Check file3if (count($file3) > 0) {//do your code to move and rename file1}// Check file4if (count($file4) > 0) {//do your code to move and rename file1}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118180 Share on other sites More sharing options...
akitchin Posted November 2, 2006 Share Posted November 2, 2006 the count method will not work, as the $_FILES array is sent whether the files are empty or not.files uploaded are located in the $_FILES array, NOT IN THE $_POST array. to check if a file has been selected or uploaded or not, all you have to do is check the filename:[code]if (empty($_FILES['input_name']['name'])){ no file uploaded for input_name}[/code]refine your question if this doesn't answer it. Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118190 Share on other sites More sharing options...
gmwebs Posted November 2, 2006 Share Posted November 2, 2006 Yep, Akitchin you are right... Thanks for putting us straight... I have included a script which will test exactly what you are saying...[code]<?php// Fetch the file array for file1$file1 = $_FILES['file1'];// Fetch the file array for file2$file2 = $_FILES['file2'];// Fetch the file array for file3$file3 = $_FILES['file3'];// Fetch the file array for file4$file4 = $_FILES['file4'];// Check file1if (!empty($file1['name'])) {//do your code to move and rename file1 echo "File1 Exists | ";} else { echo "File1 does not exist | ";}// Check file2if (!empty($file2['name'])) {//do your code to move and rename file1 echo "File2 Exists | ";} else { echo "File2 does not exist | ";}// Check file3if (!empty($file3['name'])) {//do your code to move and rename file1 echo "File3 Exists | ";} else { echo "File3 does not exist | ";}// Check file4if (!empty($file4['name'])) {//do your code to move and rename file1 echo "File4 Exists | ";} else { echo "File4 does not exist | ";}?><form enctype="multipart/form-data" method="post" name="upload_form"><label for="file1">File 1: </label><input type="file" name="file1"></input><br /><br /><label for="file1">File 2: </label><input type="file" name="file2"></input><br /><br /><label for="file1">File 3: </label><input type="file" name="file3"></input><br /><br /><label for="file1">File 4: </label><input type="file" name="file4"></input><br /><br /><input type="submit" value="upload"></form>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118313 Share on other sites More sharing options...
Ninjakreborn Posted November 2, 2006 Author Share Posted November 2, 2006 100% perfect. Exactly what I needed. I owe this one thank's to you, I was having a hard time explaining it. Thanks, if you hadn't of then I would have been fighting with this awhile.Thanks again this is exactly what I needed. Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118379 Share on other sites More sharing options...
Ninjakreborn Posted November 2, 2006 Author Share Posted November 2, 2006 so me doing $file1 = $_FILES['file1'];the I can access all the variables using like$file['file1']['name']and $file1['file1']['tmp_name']like thatis that why you grabbed them all. Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118388 Share on other sites More sharing options...
gmwebs Posted November 2, 2006 Share Posted November 2, 2006 Well instead of using the long [code=php:0]$_FILES['file1']['name'][/code] by assigning a variable to the respective [code=php:0]$_FILES['input_name'][/code] you are able to access each attribute of the respective variable directly [code=php:0]$file1['tmp_name'][/code]. I hope that explains it? You certainly don't [i]have[/i] to use my method, I just find it easier to work with. Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118409 Share on other sites More sharing options...
Ninjakreborn Posted November 2, 2006 Author Share Posted November 2, 2006 Of course I will, it make's less typing, I was just making sure I understood it right. Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118555 Share on other sites More sharing options...
roopurt18 Posted November 2, 2006 Share Posted November 2, 2006 I find it ironic you'd want to assign $_FILES['form_param'] to another variable to save typing yet you didn't want to use a single loop and would rather type the same code thirty times over.World works in mysterious ways I guess!;) Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118559 Share on other sites More sharing options...
gmwebs Posted November 2, 2006 Share Posted November 2, 2006 lol... In all fairness it was my code that suggested he assign a variable to [code=php:0]$_FILES['input_name'][/code] but yeah, fair point ;) Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118664 Share on other sites More sharing options...
Ninjakreborn Posted November 2, 2006 Author Share Posted November 2, 2006 The reason I am not, is because I don't know how. I have "always" had trouble's with file uploads. Every single time I try I run into problems' I think doing array uploads will make it even harder for me specifically until I master how they work.For instance, right now I ran into a problem[code]<?php$target = "/oakley/propimages/"; // prepare target url this is the same for all images,(since it// is the same location on all images)$file1 = $_FILES['file1'];if (!empty($file1['name'])) { $tmp_name1 = $file1['tmp_name']; $name1 = $id . "_" . $file1['name']; $target1 = $target . $name1; if (file_exists($target1)) { echo "File 1 already exists.<br />"; }else { if (move_uploaded_file($tmp_name1, $target1)) { echo "File was uploaded successfully.<br />"; }else { echo "There was a problem uploading this specific file.<br />"; echo $tmp_name1; echo $name1; echo $target1; } }} else { echo "File1 does not exist | ";}?>[/code]Once I get this working I am going to replicate it for the rest.I tried this one code, the reason I am doing it this way, I have to rename the code for each image to the propertynumber_imagenameI am having to do that on all of htem, I used that code up there, and tested it out on the first upload, and got this error.[quote]Status on File 1Warning: move_uploaded_file(/oakley/propimages/1_cup.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /homepages/30/d162063315/htdocs/oakley/admin/addimages2.php on line 77Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php3H9i9j' to '/oakley/propimages/1_cup.jpg' in /homepages/30/d162063315/htdocs/oakley/admin/addimages2.php on line 77There was a problem uploading this specific file./tmp/php3H9i9j1_cup.jpg/oakley/propimages/1_cup.jpg[/quote]the variable echo's that was only for debugging purposes by the way Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118674 Share on other sites More sharing options...
kenrbnsn Posted November 2, 2006 Share Posted November 2, 2006 The first warning message is telling you that the destination directory (or file) is not writable. Does the directory "[b]/oakley/propimages/[/b]" exist? Is it writable by everyone?I believe you really want the directory "[b][color=blue]oakley/propimages/[/color][/b]". Notice the first slash is missing, which makes the directories subdirectories of the current working directory.Ken Quote Link to comment https://forums.phpfreaks.com/topic/25869-testing-for-files-existence/#findComment-118685 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.