Jump to content

Image upload


Recommended Posts

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.


Link to comment
Share on other sites

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 by phppup
Link to comment
Share on other sites

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:


instead of what you expect to see.  Move the quotes to where they should be.


Link to comment
Share on other sites

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'];


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?



yez, I h@te it soooo much.


Edited by phppup
Link to comment
Share on other sites

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:



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]}";


Link to comment
Share on other sites

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.


Link to comment
Share on other sites

What does your html look like for this input tag?  When I research the $_FILES array I get the following as the elements:







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'];


Link to comment
Share on other sites

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:

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.

Link to comment
Share on other sites

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?


Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.