Jump to content

testing for file's existence


Ninjakreborn

Recommended Posts

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 number
on the other page I am testing for the upload with
if (isset($_POST['file1'])) {
// file was uploaded
}
Link to comment
Share on other sites

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

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 go
if (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?
Link to comment
Share on other sites

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].
Link to comment
Share on other sites

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 work
if (is_uploaded_file($_FILES['file1'])) {
// validate file
// rename file
// move file
}
then if it didn't then it just ignores that block of code?
Link to comment
Share on other sites

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

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 this

if (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.
Link to comment
Share on other sites

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 uploaded
if (count($file1) > 0) {
//do your code to move and rename file1
}

//repeat many times over... ;)
?>
[/code]
Link to comment
Share on other sites

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

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 file1
if (count($file1) > 0) {
//do your code to move and rename file1
}

// Check file2
if (count($file2) > 0) {
//do your code to move and rename file1
}

// Check file3
if (count($file3) > 0) {
//do your code to move and rename file1
}

// Check file4
if (count($file4) > 0) {
//do your code to move and rename file1
}

?>
[/code]
Link to comment
Share on other sites

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

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 file1
if (!empty($file1['name'])) {
//do your code to move and rename file1
echo "File1 Exists | ";
} else {
echo "File1 does not exist | ";
}

// Check file2
if (!empty($file2['name'])) {
//do your code to move and rename file1
echo "File2 Exists | ";
} else {
echo "File2 does not exist | ";
}

// Check file3
if (!empty($file3['name'])) {
//do your code to move and rename file1
echo "File3 Exists | ";
} else {
echo "File3 does not exist | ";
}

// Check file4
if (!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]
Link to comment
Share on other sites

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

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_imagename
I 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 1

Warning: 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 77

Warning: 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 77
There 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
Link to comment
Share on other sites

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.