Coder Profile - Show off your skills, get a coder profile.
 
 
 
  
Posted: 3.68 Years Ago 

BlindToSound
United States
Contrib Level: 2
Total Posts: 20
I have this code for my topic image upload:
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. <form method="post" enctype="multipart/form-data">
  2. <table align="center">
  3.        <tr>
  4.            <td>Title:</td>
  5.         <td><input type="text" name="title" style="width: 500px;" maxlength="40" /></td>
  6.     </tr>
  7.     <tr>
  8.            <td>Message:</td>
  9.         <td><textarea name="body" style="width: 500px; height: 250px;" maxlength="5000" /></textarea></td>
  10.     </tr>
  11.     <tr>
  12.            <td>Include Image?:
  13.            <td><input type="file" name="data" id="file" /></td>
  14.     </tr>
  15.     <tr>
  16.            <td><input type="submit" name="submit" value="Post it!" /></td>
  17.     </tr>
  18. </table>
  19. </form>
CODE: Copy / Restore  ::  Remove Scroll Bars
  1.               if(isset($_POST['data'])){
  2.                      $max_filesize = 999999;
  3.                      $filetypes = array('image/jpg','image/jpeg','image/JPG','image/JPEG', 9;image/gif','image/GIF','image/png','image/PNG','image/tiff ','image/TIFF','image/bmp','image/BMP');
  4.                      $upload_path = '/uploads/';
  5.  
  6.                      # Check to see if the filesize is too large
  7.                      if ($_FILES['data']['size'] > $max_filesize) {
  8.                             die('Your filesize is too large. Please make your filesize smaller than ' . $max_filesize . ' bytes. ( 10mb )');
  9.                      }
  10.                      // End if filesize is too large
  11.  
  12.                      list($height, $width) = getimagesize($FILES['data']['name']);
  13.  
  14.                      # Check to see if the filetype is correct
  15.                      if (!in_array($_FILES['data']['type'], $filetypes)) {
  16.                             die('Sorry, your file was not a jpg, jpeg, gif, png, tiff, or a bmp. (Yours was ' . $_FILES['data']['type'] . ')');
  17.                      }
  18.                      // End filetype check
  19.  
  20.                      # If file has gotten this far, it is successful
  21.                      $where = $_SERVER['DOCUMENT_ROOT'] . $upload_path . $_FILES['data']['name'];
  22.                      $file = 'http://www.blindtosound.com' . $upload_path . $_FILES['data']['name'];
  23.                      # Upload the file
  24.                      $upload = move_uploaded_file($_FILES['data']['tmp_name'], $where);
  25.  
  26.                      # Check to see if upload was successful
  27.                      if (!$upload) {
  28.                             die('Sorry, your file could not be uploaded.');
  29.                      }
  30.               }
I'm having 2 problems with this.
1. It's not uploading the file
2. It's not inserting the image path into the database (That part comes later in the code..)

It's not returning any errors, either.
Posted: 3.68 Years Ago 

VBAssassin
United Kingdom
Contrib Level: 17
Total Posts: 5,730
Hi ya,

It returned loads of errors. I just followed the errors and fixed them line by line.
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. <?php
  2.  
  3.        if(isset($_FILES['data'])){
  4.  
  5.               $max_filesize = 999999;
  6.               $filetypes = array('image/jpg','image/jpeg','image/JPG','image/JPEG', 9;image/gif','image/GIF','image/png','image/PNG','image/tiff ','image/TIFF','image/bmp','image/BMP');
  7.               $upload_path = '/uploads/';
  8.  
  9.               # Check to see if the filesize is too large
  10.               if ($_FILES['data']['size'] > $max_filesize) {
  11.                      die('Your filesize is too large. Please make your filesize smaller than ' . $max_filesize . ' bytes. ( 10mb )');
  12.               }
  13.               // End if filesize is too large
  14.  
  15.               list($height, $width) = getimagesize($_FILES['data']['tmp_name']);
  16.  
  17.               # Check to see if the filetype is correct
  18.               if (!in_array($_FILES['data']['type'], $filetypes)) {
  19.                      die('Sorry, your file was not a jpg, jpeg, gif, png, tiff, or a bmp. (Yours was ' . $_FILES['data']['type'] . ')');
  20.               }
  21.  
  22.               # Upload the file
  23.               $filename = mt_rand(); //preappend file with a random number to reduce chance of another file being uploaded with the same name.
  24.               $upload = move_uploaded_file($_FILES['data']['tmp_name'], 'uploads/' . $filename . '_' . $_FILES['data']['name']);
  25.  
  26.               # Check to see if upload was successful
  27.               if (!$upload) {
  28.                      die('Sorry, your file could not be uploaded.');
  29.               }
  30.        }
  31.  
  32. ?>
  33. <form method="post" enctype="multipart/form-data">
  34. <table align="center">
  35.        <tr>
  36.            <td>Title:</td>
  37.         <td><input type="text" name="title" style="width: 500px;" maxlength="40" /></td>
  38.     </tr>
  39.     <tr>
  40.            <td>Message:</td>
  41.         <td><textarea name="body" style="width: 500px; height: 250px;" maxlength="5000" /></textarea></td>
  42.     </tr>
  43.     <tr>
  44.            <td>Include Image?:
  45.            <td><input type="file" name="data" id="file" /></td>
  46.     </tr>
  47.     <tr>
  48.            <td><input type="submit" name="submit" value="Post it!" /></td>
  49.     </tr>
  50. </table>
  51. </form>
Since you said there were no errors showing, make sure you have not turned off the display of errors in your php.ini file.

Kind regards,
Scott
Posted: 3.68 Years Ago 

BlindToSound
United States
Contrib Level: 2
Total Posts: 20
Ahhh, thanks so much, Scott.

As for my php.ini file, I haven't touched it at all since I got my hosting..So it's still the default, I guess
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. register_globals = Off
  2.  
  3.  
  4.  
  5.  
  6.  
  7. zend_extension="/usr/local/IonCube/ioncube_loader_lin_4.4.so"
  8. zend_extension_ts="/usr/local/IonCube/ioncube_loader_lin_4.4_ts.so"
  9.  
  10. [Zend]
  11. zend_optimizer.optimization_level=15
  12. zend_extension_manager.optimizer=/usr/local/Zend/lib/Optimizer-3.3.3
  13. zend_extension_manager.optimizer_ts=/usr/local/Zend/lib/Optimizer_TS-3.3.3
  14. zend_optimizer.version=3.3.3
  15.  
  16. zend_extension=/usr/local/Zend/lib/ZendExtensionManager.so
  17. zend_extension_ts=/usr/local/Zend/lib/ZendExtensionManager_TS.so
Posted: 3.68 Years Ago 

VBAssassin
United Kingdom
Contrib Level: 17
Total Posts: 5,730
http://www.washington.edu/computing/web/publishing/php-ini.html#display_err...

And it's the last part about displaying errors.

Kind regards,
Scott
Posted: 3.67 Years Ago 

BlindToSound
United States
Contrib Level: 2
Total Posts: 20
Well, Scott. That doesn't exactly work the way I had wanted it to. It allows the image to be posted IF there's an image in the field. If there's not an image in the field to be uploaded, it returns errors. I only want the image upload code to run if there's something in the field to be uploaded. =/
CODE: Copy / Restore  ::  Remove Scroll Bars
  1.        if(isset($_FILES['data'])){
  2.  
  3.               $max_filesize = 999999;
  4.               $filetypes = array('image/jpg','image/jpeg','image/JPG','image/JPEG', 9;image/gif','image/GIF','image/png','image/PNG','image/tiff ','image/TIFF','image/bmp','image/BMP');
  5.               $upload_path = '/uploads/';
  6.  
  7.               # Check to see if the filesize is too large
  8.               if ($_FILES['data']['size'] > $max_filesize) {
  9.                      die('Your filesize is too large. Please make your filesize smaller than ' . $max_filesize . ' bytes. ( 10mb )');
  10.               }
  11.               // End if filesize is too large
  12.  
  13.               list($height, $width) = getimagesize($_FILES['data']['tmp_name']);
  14.  
  15.               # Check to see if the filetype is correct
  16.               if (!in_array($_FILES['data']['type'], $filetypes)) {
  17.                      die('Sorry, your file was not a jpg, jpeg, gif, png, tiff, or a bmp. (Yours was ' . $_FILES['data']['type'] . ')');
  18.               }
  19.  
  20.               # Upload the file
  21.               $filename = mt_rand(); //preappend file with a random number to reduce chance of another file being uploaded with the same name.
  22.               $upload = move_uploaded_file($_FILES['data']['tmp_name'], 'uploads/' . $filename . '_' . $_FILES['data']['name']);
  23.               $file = "http://www.blindtosound.com/uploads/". $filename . '_' . $_FILES['data']['name'];
  24.               # Check to see if upload was successful
  25.               if (!$upload) {
  26.                      die('Sorry, your file could not be uploaded.');
  27.               }
  28.        }
It works wonderfully IF there's an image in the field, but when they're not, it returns the errors anyways as if there was.
Posted: 3.67 Years Ago 

Relish
United States
Contrib Level: 9
Total Posts: 799
also check for $_POST['data'] to check that the forms been filled in
Posted: 3.67 Years Ago 

BlindToSound
United States
Contrib Level: 2
Total Posts: 20
I tried both $_POST and $_FILES
Posted: 3.67 Years Ago 

Relish
United States
Contrib Level: 9
Total Posts: 799
Not using isset, use empty

if (empty($_POST['data'])) {....
Posted: 3.67 Years Ago 

VBAssassin
United Kingdom
Contrib Level: 17
Total Posts: 5,730
Of course it does, there's no error checking.

Check $_FILES['data']['error']

If it == 4 then no file was uploaded

http://uk2.php.net/manual/en/features.file-upload.errors.php

Kind regards,
Scott
Posted: 3.67 Years Ago 

BlindToSound
United States
Contrib Level: 2
Total Posts: 20
Still not working. I'll send the whole page, because I'm confused now..
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. <?php
  2. include("connect.php");
  3. include("functions.php");
  4. if(!isset($_SESSION['logged'])){
  5.        $_SESSION['error'] = "You need to be logged in to post a topic";
  6.        header("Location: http://www.blindtosound.com/");
  7. }
  8. if(isset($_POST['submit'])){
  9.        if(isset($_SESSION['logged'])){
  10.        $username = mysql_real_escape_string($_SESSION['logged']);
  11.        $title = htmlentities(mysql_real_escape_string($_POST['title']));
  12.        $body = htmlentities(mysql_real_escape_string($_POST['body']));
  13.  
  14.        // ----------------- Errors ----------------
  15.  
  16.        if(empty($title) || empty($body)){
  17.               $empty = true;
  18.        }
  19.        if(preg_match("/[^\sa-zA-Z0-9-?!&'\":,.]/", $title)){
  20.               $char = true;
  21.        }
  22.        if($empty == true){
  23.               echo "<div class='error'>" . error("You need to fill in all of the forms!") . "</div>";
  24.        }
  25.        if($char == true){
  26.               echo "<div class='error'>" . error("The title's characters need to be a-z, A-Z, 0-9, hyphens, question marks, and spaces!") . "</div>";
  27.        }
  28.  
  29.        // --------------- End of Errors -------------
  30.  
  31.        // ----------------- Start of File Upload --------------------
  32.  
  33.        if(!empty($_FILES['data'])){
  34.  
  35.               $max_filesize = 999999;
  36.               $filetypes = array('image/jpg','image/jpeg','image/JPG','image/JPEG', 9;image/gif','image/GIF','image/png','image/PNG','image/tiff ','image/TIFF','image/bmp','image/BMP');
  37.               $upload_path = '/uploads/';
  38.  
  39.               # Check to see if the filesize is too large
  40.               if ($_FILES['data']['size'] > $max_filesize) {
  41.                      die('Your filesize is too large. Please make your filesize smaller than ' . $max_filesize . ' bytes. ( 10mb )');
  42.               }
  43.               // End if filesize is too large
  44.  
  45.               list($height, $width) = getimagesize($_FILES['data']['tmp_name']);
  46.  
  47.               # Check to see if the filetype is correct
  48.               if (!in_array($_FILES['data']['type'], $filetypes)) {
  49.                      die('Sorry, your file was not a jpg, jpeg, gif, png, tiff, or a bmp. (Yours was ' . $_FILES['data']['type'] . ')');
  50.               }
  51.  
  52.               # Upload the file
  53.               $filename = mt_rand(); //preappend file with a random number to reduce chance of another file being uploaded with the same name.
  54.               $upload = move_uploaded_file($_FILES['data']['tmp_name'], 'uploads/' . $filename . '_' . $_FILES['data']['name']);
  55.               $file = "http://www.blindtosound.com/uploads/". $filename . '_' . $_FILES['data']['name'];
  56.               # Check to see if upload was successful
  57.               if (!$upload) {
  58.                      die('Sorry, your file could not be uploaded.');
  59.               }
  60.        }
  61.  
  62.        // ------------------ End of File Upload ---------------------
  63.  
  64.        mysql_query("INSERT INTO `topics` (`title`, `body`, `username`, `topbump`, `lastpost`, `image`, `imgheight`, `imgwidth`) VALUES ('$title', '$body', '$username', '$topbump', '$username', '$file', '$height', '$width')") or die(mysql_error());
  65.        mysql_query("UPDATE `users` SET `postcount` = `postcount` + 1 WHERE `username` = '$username'") or die(mysql_error());
  66.        $topbump = mysql_insert_id();
  67.        $redir = mysql_fetch_array(mysql_query("SELECT * FROM `topics` ORDER BY `id` DESC LIMIT 1")) or die(mysql_error());
  68.        $redir = $redir['id'];
  69.        mysql_query("UPDATE `topics` SET `topbump` = '$topbump' WHERE `id` = '$redir'") or die(mysql_error());
  70.        header("Location: http://www.blindtosound.com/topic/$redir");
  71.        }
  72. }
  73. mysql_close();
  74. ob_end_flush();
  75. ?>
Posted: 3.67 Years Ago 

VBAssassin
United Kingdom
Contrib Level: 17
Total Posts: 5,730
CODE: Copy / Restore  ::  Remove Scroll Bars
  1.        if(isset($_FILES['data'])){
  2.  
  3.               $max_filesize = 999999;
  4.               $filetypes = array('image/jpg','image/jpeg','image/JPG','image/JPEG', 9;image/gif','image/GIF','image/png','image/PNG','image/tiff ','image/TIFF','image/bmp','image/BMP');
  5.               $upload_path = 'uploads/';
  6.  
  7.               #make sure a file was uploaded
  8.               if ($_FILES['data']['error'] != 4) {
  9.  
  10.                      # Check to see if the filesize is too large
  11.                      if ($_FILES['data']['size'] > $max_filesize) {
  12.                             die('Your filesize is too large. Please make your filesize smaller than ' . $max_filesize . ' bytes. ( 10mb )');
  13.                      }
  14.                      // End if filesize is too large
  15.  
  16.                      list($height, $width) = getimagesize($_FILES['data']['tmp_name']);
  17.  
  18.                      # Check to see if the filetype is correct
  19.                      if (!in_array($_FILES['data']['type'], $filetypes)) {
  20.                             die('Sorry, your file was not a jpg, jpeg, gif, png, tiff, or a bmp. (Yours was ' . $_FILES['data']['type'] . ')');
  21.                      }
  22.  
  23.                      # Upload the file
  24.                      $filename = mt_rand(); //preappend file with a random number to reduce chance of another file being uploaded with the same name.
  25.                      $upload = move_uploaded_file($_FILES['data']['tmp_name'], 'uploads/' . $filename . '_' . $_FILES['data']['name']);
  26.  
  27.                      # Check to see if upload was successful
  28.                      if (!$upload) {
  29.                             die('Sorry, your file could not be uploaded.');
  30.                      }
  31.  
  32.                      } else {
  33.  
  34.                      print 'No file was selected to upload';
  35.  
  36.               }
  37.  
  38.  
  39.        }
Page of 2 :: Next Page >>
 
 
Latest News About Coder Profile
Coder Profile Poll
Why do you get bored with programming?

Not enough time to do something productive
I run out of ideas
Too hard to show people my creations
Everything i do has too many errors, and it's too hard
I don't get bored!!!


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
3.49 Years Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2012, Scott Thompson, All Rights Reserved