Jump to content

File Upload


Recommended Posts

 Ok I have some code that's really starting to bug me... The form right under posts to a page and php picks it up and processes it. My problem is that the php will pick up the data and process it if the fields are not filled in or if only one but not both file input are selected. I want to be able to upload both at the same time.The other $POST variable post just fine but not when both $FILES are selected.

P.s. I cleaned up the code a bit so its easier to read its not exactly how it is on my site please help!

<form action="/administration/xxupload/" method="post" enctype="multipart/form-data">
<input type="hidden" name="artist_id" value="<?php echo $id; ?>" />
</span><input type="text" name="title" id="title" />
<span>Artwork:</span><input type="file" name="art" id="artwork"/>
<span>Song:</span><input type="file" name="content" id="content"/>
><span>Details:</span><br/><textarea id="detail" name="detail" ></textarea>
<input type="hidden" name="type" value="1" />
<input type="hidden" name="user" value="Admin" />
<input type="hidden" name="time" value="<?php echo time() ?>" />
<div id="tagsdiv">Tags:<input type="text" name="tags" /></div>
<div id="submit"><input type="submit" name="upload_single"/></div>

Part of xxupload.php


$artist_display=mysql_fetch_assoc(mysql_query("SELECT name FROM artists WHERE id=".$artist_id));





("INSERT INTO media(artist_id,title, art, content, detail,tags,user,type,tic)

   header( 'Location: /administration/' ) ;


Link to comment
Share on other sites

a) you didn't state exactly what your code does when both files are chosen. stating it doesn't process them isn't specific enough as that doesn't tell us what execution path your code takes. what exactly does happen/what is the symptom when this occurs?


b) your code doesn't have any logic in it to test if the upload worked before trying to use any of the uploaded file information. in programming, you must always test if a step that can fail has worked or not before using the data that you expected from that step. the size of the uploaded files is not under the control of your code, therefore they can be anything and since php limits both the size of each individual file and of the total amount of data sent from a form, you must test that there is form data to process before you attempt to use it.


because php (tries to) abort the upload when you exceed the post_max_size setting, there is no $_POST or $_FILES data in this case and you should test if($_SERVER['REQUEST_METHOD'] == 'POST') to detect if an upload form was submitted.


if all you want to do after that is make sure the file was uploaded, test if the error element is set and is equal to a zero if(isset($_FILES['art']['error']) && $_FILES['art']['error'] == 0){ code that uses the $_FILES['art'] data }.


if you want to output a specific message for this case, you can test if the value in $_SERVER['CONTENT_LENGTH'] is greater than the post_max_size setting. if the total of all the files doesn't exceed the the post_max_size setting, but any individual file is still too large, the ['error'] element will be a 1 (see this link for all the upload error values - http://www.php.net/manual/en/features.file-upload.errors.php ). you can output specific error messages when any of these errors are detected to let the user know why their upload didn't work.

Link to comment
Share on other sites

Yeah I'm sorry I guess i wasn't that clear in what i wanted to do. I'm trying to upload files to the server, one img and a mp3 file.

Then rename and insert information about the uploaded files into a mysql database.


I've since tried uploading smaller files and the script does work properly so now i know its a problem with the total size or individual sizes of the upload.

I've changed a couple of things in php.ini file like the upload_max_filesize and the post_max_size but i doesnt seems to make a difference. Anyone know what i should change to get the file upload to function with larger files?

Link to comment
Share on other sites

Hey sry I wanted to try a few more things before and I played around with some settings again in the php.ini file. It works now. I have to admit i don't know much about error reporting or about php.ini.


Do you know of any videos or good articles that explain either?

Edited by mindblown
Link to comment
Share on other sites

Do you know of any videos or good articles that explain either?


As a web developer, you will often develop your scripts on unix (win) machines, web servers, database severs, ftp servers, mail servers and so on, so on.

The best way for me and for everybody I guess is to start reading and understanding the error log files.

Every web application above has a particular error directory on the real server.

Go there, read the error message, if you don't understand it, go to the web site of this application or "google it" and try to find out what this error causes.

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.

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.