473,378 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to handle an uploaded file without using separate script file

14
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:

Expand|Select|Wrap|Line Numbers
  1. <form method="POST" enctype="multipart/form-data" name="image_upload_form" action="">
  2. <p><input type="file" name="image_file" size="20"></p>
  3. <p><input type="submit" value="Upload Image" name="action"></p>
  4. </form>
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:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/template.dwt.php" codeOutsideHTMLIsLocked="false" -->
  4.  
  5. <head>
  6. <script type="text/JavaScript">
  7.  
  8. <? php
  9.  
  10. define ('MAX_FILE_SIZE', 1024 * 50);
  11.  
  12. if (array_key_exists('upload', $_POST)) {
  13.   // replace any spaces in original filename with underscores
  14.   $file = str_replace(' ', '_', $_FILES['image']['name']);
  15.   // create an array of permitted MIME types
  16.   $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg',
  17. 'image/png');
  18.  
  19.   // upload if file is OK
  20.   if (in_array($_FILES['image']['type'], $permitted)
  21.       && $_FILES['image']['size'] > 0 
  22.       && $_FILES['image']['size'] <= MAX_FILE_SIZE) 
  23.     {
  24.       $result= "$file file uploaded!";
  25.     }
  26.   else {
  27.     $result = "$file is either too big or not an image.";
  28.   }
  29.  
  30.  
  31. }
  32. ?>
  33.  
  34.  
  35.  
  36. </script>
  37. </head>
  38. <body>
  39.  
  40. <?php
  41. // if the form has been submitted, display result
  42. if (isset($result)) {
  43.   echo "<p><strong>$result</strong></p>";
  44. }
  45. ?>
  46.  
  47. <form method="POST" enctype="multipart/form-data" name="image_upload_form" action="">
  48. <p><input type="file" name="image_file" size="20"></p>
  49. <p><input type="submit" value="Upload Image" name="action"></p>
  50. </form>
  51.  
  52. </body>
  53. </html> 

Thanks!
Oct 20 '10 #1
5 1968
code green
1,726 Expert 1GB
I think what you are trying to do is get the same page to submit itself.
You do this with
Expand|Select|Wrap|Line Numbers
  1. action="$_SERVER['PHP_SELF']"
Oct 20 '10 #2
LELE7
14
I'm getting the following error:
Firefox can't find the file at
/C:/WebSites/STUNNI~1/$_SERVER['PHP_SELF'], the root directory being the root of my page. Why does it think it's a separate file? Am I missing something important in my code on top?
Oct 21 '10 #3
code green
1,726 Expert 1GB
I think your HTML is wrong. I would need to see it.
But $_SERVER['PHP_SELF'] returns
The filename of the currently executing script, relative to the document root.
For instance, $_SERVER['PHP_SELF'] in a script at the address
http://example.com/test.php/foo.bar would be /test.php/foo.bar.
Oct 21 '10 #4
HaLo2FrEeEk
404 256MB
If it's in HTML, then he needs to do:

action="<?php echo $_SERVER['PHP_SELF']; ?>"

That should work.
Oct 21 '10 #5
LELE7
14
In the end, I think the problem was that I wasn't running it from the proper place- from my localhost (or my WAMP/www directory. I was just running it from my regular file directory. Not sure if this makes sense, but I'm not getting the error anymore. Thanks for your help!
Oct 22 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: balgach | last post by:
Greetings all, I have a group of rather large files (by group i mean close to 2x10^7 files, each 12-15megs) now i need information which is stored in just the last 512 bytes of each file. i...
11
by: ulyses | last post by:
Let's assume I have following file: 2938929384902491233..... 923949919199191919112.... File contains INTs only. What is more they are huge. For example first row in file may contain integer...
4
by: gsuns82 | last post by:
hi friends,can any one give the coding for finding file size using java script???
5
by: sat | last post by:
Hi, I'm working as a DB2 UDB DBA. I have some questions regarding SCP command... Actually every week redirect restore takes place from one of my production servers to test servers.. So, but...
2
by: rinkudhimar | last post by:
I want to write using fwrite and upload the file without using File Field or Browse option. Actually I want to upload static file after writing it useing fwrite and as soon as it gets uploaded it...
5
by: ganesh.kundapur | last post by:
Hi, I want to know how to compile the C source file without using the compiler directly. I mean to say by using compiler componets such as ( cpp, cc, as, ld ). I tried to compile the fallowing...
6
by: Gorksha | last post by:
How to upload a file without using browse button provided by <input type=file > tag in html..
6
by: Kuldeep | last post by:
Framework: Visual Studio 2005 Technology: ASP.NET 2.0 Language: C#.NET 2.0 Hi All, How can we generate an Excel File through C#.NET without using Interops or XML ? Any leads on this would...
1
by: mihir0288 | last post by:
I want to save HTML file that i open in web browser on my hard disk without using save file dialog box. I want to save it as html page through only code and without user interactions.
3
by: nguyenthuy | last post by:
How to disable Mousewhell without using MouseWheel.dll file and copy it to each computer need to prevent scrolling. If there are many computer use the program, I can't setup one bye one. So, please...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.