phppup Posted November 20, 2019 Share Posted November 20, 2019 I have a PHP script that modifies images that are stored in a local folder related to $startingFolder. Essentially, I can use $startingFolder = 'anyFolder/'; $finalFolder = 'endResult/'; To manage the variables and direct the source and destination of the scripts actions from this starting point. I'm trying to extend my capabilities so that I can use the script while uploading images. Rather than UPLOAD several images to $startingFolder and then run the script, I thought it would be more efficient to handle this in one script. However, I am having trouble making the CONNECTION so that this can be accomplished. What is the proper way to 'grab' the files during upload? How can I access the files during the process? I have a working HTML <input type='file' name='upload[]' multiple > And have tried $startingFolder = '$_FILE[upload]'; but I am missing the mark somewhere. Please help. Quote Link to comment https://forums.phpfreaks.com/topic/309556-image-upload/ Share on other sites More sharing options...
gw1500se Posted November 20, 2019 Share Posted November 20, 2019 Reading the manual will help a lot. Quote Link to comment https://forums.phpfreaks.com/topic/309556-image-upload/#findComment-1571732 Share on other sites More sharing options...
Barand Posted November 20, 2019 Share Posted November 20, 2019 Plus a couple of related sections... Handling file uploads Uploading multiple files Quote Link to comment https://forums.phpfreaks.com/topic/309556-image-upload/#findComment-1571733 Share on other sites More sharing options...
ginerjm Posted November 20, 2019 Share Posted November 20, 2019 As well as putting your array indices in quotes as in $_FILE['upload'] Quote Link to comment https://forums.phpfreaks.com/topic/309556-image-upload/#findComment-1571734 Share on other sites More sharing options...
phppup Posted November 20, 2019 Author Share Posted November 20, 2019 (edited) Thanks to all, but I am still confused. I have read both Barand's references earlier, and in fact have quotes on my array index. Am I correct in expecting $startingFolder = '$_FILE['upload']; to work as a valid replacement to $startingFolder = 'LocalFolder/' or am I missing something. Isn't the code that effectively loops through the folder's files adequate to loop through the array? Should I be linking to the tmp folder instead? Edited November 20, 2019 by phppup Quote Link to comment https://forums.phpfreaks.com/topic/309556-image-upload/#findComment-1571741 Share on other sites More sharing options...
ginerjm Posted November 20, 2019 Share Posted November 20, 2019 This line: $startingFolder = '$_FILE[upload]'; is TOTALLY incorrect. In fact you are assigning the following text value "$_FILE[upload]" to the variable $startingFolder. If you added this line echo $startingFolder; following the above line you would see: $_FILE[upload] instead of what you expect to see. Move the quotes to where they should be. Quote Link to comment https://forums.phpfreaks.com/topic/309556-image-upload/#findComment-1571742 Share on other sites More sharing options...
phppup Posted November 20, 2019 Author Share Posted November 20, 2019 (edited) In actuality the quotes are in the correct location. It's the damn software that I am trying to use to create the post that keeps shifting things. $startingFolder = $_FILE['upload']; ['justsinglequotesethere'] But I still am not getting any results. PS: should echo $startingFolder give me the name of the tmp folder or list its contents? What do I need to complete the connection? I HATE AUTOCORRECT. yez, I h@te it soooo much. Edited November 20, 2019 by phppup Quote Link to comment https://forums.phpfreaks.com/topic/309556-image-upload/#findComment-1571743 Share on other sites More sharing options...
cyberRobot Posted November 20, 2019 Share Posted November 20, 2019 Be aware that the global variable that you are looking for is called $_FILES. Note the "S" at the end. Also, the name of your input field is "upload[]". That causes PHP to create an array for you, which is good if you're looking to upload multiple files. However, the syntax for accessing the file information will be different. For example, to access the name of the first uploaded file, you would use the following: $_FILES['upload']['name'][0] Side note: variable don't work when they are enclosed in single quoted strings. When assigning whatever value you want to $startingFolder, you need to remove the outer quotes $startingFolder = $_FILES['upload']['name'][0]; ...or you could use double quotes $startingFolder = "{$_FILES['upload']['name'][0]}"; Quote Link to comment https://forums.phpfreaks.com/topic/309556-image-upload/#findComment-1571744 Share on other sites More sharing options...
ginerjm Posted November 20, 2019 Share Posted November 20, 2019 Yes - you have corrected the syntax errors of your first two posts and are now referencing that item properly. Now you just have to follow the other good advice you are receiving. Quote Link to comment https://forums.phpfreaks.com/topic/309556-image-upload/#findComment-1571745 Share on other sites More sharing options...
phppup Posted November 20, 2019 Author Share Posted November 20, 2019 I am double checking my code and I have had the correct syntax in place (ie: $startingFolder = $_FILES['upload']; ) yet I am not getting a result unless a switch back to $startingFolder = 'myLocalstorage/'; (at which point everything runs fine. Is there a diagnostic or echo that I can use to confirm a connection to the uploading items. My upload is confirming that the items are selected, but I am not receiving a success message nor am I seeing a result in my destination folder when I upload. Quote Link to comment https://forums.phpfreaks.com/topic/309556-image-upload/#findComment-1571750 Share on other sites More sharing options...
ginerjm Posted November 21, 2019 Share Posted November 21, 2019 What does your html look like for this input tag? When I research the $_FILES array I get the following as the elements: name tmp_name size type error I get no "upload" element. I can only assume that 'upload' is the actual NAME of your input tag so you have to add another index to your grab to get the value you actually want. Such as $_FILES['upload']['name']; Quote Link to comment https://forums.phpfreaks.com/topic/309556-image-upload/#findComment-1571765 Share on other sites More sharing options...
cyberRobot Posted November 21, 2019 Share Posted November 21, 2019 1 hour ago, ginerjm said: What does your html look like for this input tag? Here is what was provided in the original post: <input type='file' name='upload[]' multiple > 16 hours ago, phppup said: Is there a diagnostic or echo that I can use to confirm a connection to the uploading items. You could output the $_FILES array. For example: print '<pre>' . print_r($_FILES['upload'], true) . '</pre>'; That should give you all the data sent from the fields named as "upload[]". The following page describes the elements associated with the file upload process:https://www.php.net/manual/en/features.file-upload.post-method.php The one you would be interested in is "tmp_name", which is the temporary name given by the server to the file upload. You'll need to use that to move the file to wherever you want it stored. Quote Link to comment https://forums.phpfreaks.com/topic/309556-image-upload/#findComment-1571781 Share on other sites More sharing options...
phppup Posted November 21, 2019 Author Share Posted November 21, 2019 Thanks greatly to cyberRobot. Using the diagnostic PRINT line assured me that the UPLOAD is being grabbed and effectively moving files. Now I need to focus on the code. At this point, I think these lines are where my problem lies if($dir = opendir($startingFolder)){ while(($file = readdir($dir))!== false){ The original scripting (which works) is designed to take folders from an established folder rather than from an upload. So my thinking is that I need to initiate a transition. I have tried replacing these lines with foreach ($_FILES["upload"]["error"] as $key => $error) { $tmp_name = $_FILES["upload"]["tmp_name"][$key]; if (!$tmp_name) continue; $name = basename($_FILES["upload"]["name"][$key]); if ($error == UPLOAD_ERR_OK) { but that is apparently incorrect. What coding can I use to bridge the gap to allow the uploaded files to continue through the scripted process? Quote Link to comment https://forums.phpfreaks.com/topic/309556-image-upload/#findComment-1571795 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.