I'm new to PHP, but I usually program Windows Apps in .NET(c#, VB). So please excuse my unfamiliarity with code and HTML combined.
It seems pretty simple to upload a file to a website using the following html:
I'm dealing with a website that the standard is not to use separate script files, but to code in the same file as the html. Most examples on the web for this use separate script files. I found a few examples that do not work. What am I missing in my code to somehow connect the uploaded file in the html to my code on top?
My code with html:
Thanks!
It seems pretty simple to upload a file to a website using the following html:
Code:
<form method="POST" enctype="multipart/form-data" name="image_upload_form" action=""> <p><input type="file" name="image_file" size="20"></p> <p><input type="submit" value="Upload Image" name="action"></p> </form>
My code with html:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/template.dwt.php" codeOutsideHTMLIsLocked="false" --> <head> <script type="text/JavaScript"> <? php define ('MAX_FILE_SIZE', 1024 * 50); if (array_key_exists('upload', $_POST)) { // replace any spaces in original filename with underscores $file = str_replace(' ', '_', $_FILES['image']['name']); // create an array of permitted MIME types $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'); // upload if file is OK if (in_array($_FILES['image']['type'], $permitted) && $_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) { $result= "$file file uploaded!"; } else { $result = "$file is either too big or not an image."; } } ?> </script> </head> <body> <?php // if the form has been submitted, display result if (isset($result)) { echo "<p><strong>$result</strong></p>"; } ?> <form method="POST" enctype="multipart/form-data" name="image_upload_form" action=""> <p><input type="file" name="image_file" size="20"></p> <p><input type="submit" value="Upload Image" name="action"></p> </form> </body> </html>
Thanks!
Comment