Jump to content

uploading images


kikilahooch

Recommended Posts

I'm stuck on trying to upload images onto my server. At the moment I am manually adding images by copying them into an images folder i have on my public_html. I need to be able to let other people add images as well but through a front end where they can browse their own pc and click on upload rather than being able to copy into this images folder as they wont have access to it.

The way that i am displaying images on my site is by storing just the name of the image in a field in my database, eg. sample.jpg

I am then just storing the path of the images folder and appending it to the image like


[code]$imageDir = "http://snet.wit.ie/~ccousins/clothes/";
$imagePath = $imageDir."".$image;
[/code]

This is working fine for the images that I stored manually.

To try and upload images through a front end i used the following code:

[code]
<form action="do_upload.php" enctype="multipart/form-data" method="POST">
            <input type="hidden" name="MAX_FILE_SIZE" value="51200">
        

<TD align=right><FONT color=#006699><B>File to Upload:</B></FONT></TD>
               <TD><input type="file" name="image">
               <input type="submit" value="upload!"></TD>[/code]


[code]
<?php
$file_dir = "/path/to/upload/directory";
foreach($_FILES as $file_name => $file_array) {
    echo "path: ".$file_array['tmp_name']."<br>\n";
    echo "name: ".$file_array['name']."<br>\n";
    echo "type: ".$file_array['type']."<br>\n";
    echo "size: ".$file_array['size']."<br>\n";

    if (is_uploaded_file($file_array['tmp_name'])) {
        move_uploaded_file($file_array['tmp_name'], "$file_dir/$file_array[name]") or die ("Couldn't copy");
        echo "file was moved!<br><br>";
    }
}
?>
[/code]

this is just storing the address of the image on my pc eg.G:\webpages\snet files\clothes\fen_bag.jpg

when i try to display the image then is just shows up as a missing image.

I got this code from a book so I am not too sure if it is suitable for what I need. Any help anyone??
Link to comment
Share on other sites

I am a little bit lazy, so please read this:

[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=91435\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=91435[/a]
There are multiple files, but it's the same logic.

or this:
[a href=\"http://www.php.net/manual/en/features.file-upload.php\" target=\"_blank\"]http://www.php.net/manual/en/features.file-upload.php[/a]
Link to comment
Share on other sites

Its still not saving the image into the images folder, its only storing its path in the image field in my db. I already have an admin_insert.php page that I was using the insert all of the other details. Should I have just put it at the top of that?

[code]
<?

include "db.php";


$updir = 'http://snet.wit.ie/~ciaracousins/clothes/'

for ($k=0; $k<count($_FILES['pictures']['name']); $k++) {
   if ($_FILES['pictures']['error'][$k] == '0') {
      if (move_uploaded_file($_FILES['pictures']['tmp_name'][$k],
      $updir . basename($_FILES['pictures']['name'][$k]))) {
         echo 'File ' . ($k+1) . ' uploaded successfully' . '<br />';
      }
   }
}

$submit=$HTTP_POST_VARS["submit"];
$reset=$HTTP_POST_VARS["reset"];

$shopName=$HTTP_POST_VARS["shopName"];
$prodName=$HTTP_POST_VARS["prodName"];
$dept=$HTTP_POST_VARS["dept"];
$brand=$HTTP_POST_VARS["brand"];
$type=$HTTP_POST_VARS["type"];
$image=$HTTP_POST_VARS["image"];
$price=$HTTP_POST_VARS["price"];
$NoInStock=$HTTP_POST_VARS["NoInStock"];


if (!$shopName || !$prodName || !$dept || !$brand || !$type ||
    !$image || !$price || !$NoInStock ){
    echo "Please fill all the fields";
    exit;


}


/* Performing SQL query */

$query = "select * from product where prodName = '$prodName'";

//$result = mysql_query($query,$connect);

require_once('db.php');


    $query = "INSERT INTO product Values ('','$shopName','$prodName', '$dept', '$brand', '$type','$image', '$price', '$NoInStock')";
    $result = mysql_query($query);

    if ($result){
        echo "data inserted";
        
    }
    else{
        echo "This product has already been entered !!!";
    }
    /* Free resultset */
    

    /* Closing connection */
    mysql_close();

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

I use to make some gallery many times at work, and use the same method.. save the name of the imagem at db. =) I like this, is quite simple..

About your form.. I think its better then your.. xhtml validation ;)!
[code]<form action="do_upload.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="51200" />
<TD>
  <p style="color: #006699;">
    <strong>File to Upload:</strong>
  </p>
</TD>
<TD>
  <input type="file" name="image" />
  <input type="submit" value="Upload!" />
</TD>[/code]
About your code.. try it, i write but have no time to test.. so.. do it for me ok?? =)
[code]// Folder
$file_dir = './path/to/upload/directory/';
// Get the image..
$image = $HTTP_POST_FILES['image'];
// Verify for erros
if ( $image['error'] == 0 )
{
    // Ok, next: is it uploaded?
    if ( is_uploaded_file ( $image['tmp_name'] ))
    {
        // Copy it to folder and check
        if ( move_uploaded_file ( $image['tmp_name'] , $file_dir . $image['name'] ))
        {
            // Ok, done
            # do something here, use header.. java redirection.. don't know, use your imagination..
            print 'Done! The file was uploaded!';
        }
    }
}[/code]
D.Soul
Link to comment
Share on other sites

it still isnt uploading the image. on the same page I have a form action for admin_insert.php. When I click on the upload button it brings me to admin_insert.php and displays the message "data inserted". It doesnt seem to be calling the do_upload.php file at all. Any suggestions where I am going wrong??

[code]
<P><STRONG><FONT color="#006699" size=+1><STRONG><B>Add New Records</B></FONT> </STRONG>
                <P><FONT color="#006699">Fill in the fields below </FONT></P>
                <FORM onsubmit="return ShowFormWarning()"
            action="http:admin_insert.php" method=post
            target=_blank>
                  <P>
                  <TABLE align =center width="100%" border=0>
                    <?php
      if (isset(<FORM onsubmit="return ShowFormWarning()"
))                                  
           echo "<tr><td colspan='2'><b>$message_new</b></td></tr>";
    ?>
               <form action="doupload.php" enctype="multipart/form-data" method="POST">
            <input type="hidden" name="MAX_FILE_SIZE" value="51200">
        
        <TBODY>
                  <TD align=right><FONT color="#006699"><B>Shop Name:</B></FONT></TD>
                        <TD><INPUT onfocus=ShowPwWarning() maxLength=45
                  size=45 name=shopName></TD></br>

                 <TR><TD align=right><FONT color="#333333"><B> <font color="#006699">Product Name:</font></B></TD>
                        <TD> <p>
                            <INPUT onfocus=ShowPwWarning()  maxLength=45
                  size=45 name=prodName>
                          </p></TD>
                      </TR>
                                                                       <TR>
                        <TD align=right><FONT color=#006699><B>Type:</font></B></FONT></TD>
                        <TD><INPUT maxLength=45 size=45 name=type></TD>
                      </TR>

              <?php
      if (isset(<FORM onsubmit="return ShowFormWarning()"
))                                  
           echo "<tr><td colspan='2'><b>$message_new</b></td></tr>";
    ?>
              
            

            <form action="do_upload.php" enctype="multipart/form-data" method="post">
            <input type="hidden" name="MAX_FILE_SIZE" value="51200" />
        <TD>
          <p style="color: #006699;">
            <strong>File to Upload:</strong>
          </p>
            </TD>
        <TD>
         <input type="file" name="image" />
         <input type="submit" value="Upload!" />
        </TD>
                

            
                      </TR>
               <TR>
                        <TD height="82"></TD>
                        <TD><p>
                            <INPUT type=submit value="Become a member" name=submit>
                            <INPUT type=reset value=Reset name=reset>
                          </p>
                        
                          </TD>
                      </TR></form>

            

                      
                    </TBODY>
                  </TABLE>
                </FORM>
[/code]
Link to comment
Share on other sites

Is it your code?? This code have some problems.. like it:
[b]if (isset(<FORM onsubmit="return ShowFormWarning()"
))[/b]

You open two forms, one inside other.. is it right?

I'm looking at your code and fixing what I think may I.. will post it after.. but, if it is not your code, post it.. =)

*editing* well.. the code..
[code]<p style="color: #006699; font-size: 13px;"><strong>Add New Records</strong></p>
<p style="color: #006699;">Fill in the fields below</p>
<form action="doupload.php" enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="51200" />
<table align="center" width="100%" border="0">
  <tr>
    <td align="right" style="color: #006699;">
      <strong>Shop Name:</strong>
    </td>
    <td>
      <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="shopName" />
    </td>
  </tr>
  <tr>
    <td align="right" style="color: #006699;">
      <strong>Product Name:</strong>
    </td>
    <td>
      <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="prodName" />
    </td>
  </tr>
  <tr>
    <td align="right" style="color: #006699;">
      <strong>Type:</strong>
    </td>
    <td>
      <input maxLength="45" size="45" name="type" />
    </td>
  </tr>
  <tr>
    <td align="right" style="color: #006699;">
      <strong>File to Upload:</strong>
    </td>
    <td>
      <input type="file" name="image" />
      <input type="submit" value="Upload!" />
    </td>
  </tr>
  <tr>
    <td align="center" colspan="2">
      <input type=submit value="Become a member" name=submit />
      <input type=reset value=Reset name=reset />
    </td>
  </tr>
</table>
</form>[/code]

D.Soul
Link to comment
Share on other sites

thanks for your replys. D.Soul when I tried that code i got the following error msg:

Warning: move_uploaded_file(/home/c/ciaracousins/public_html/clothes/baggy.jpg): failed to open stream: Permission denied in /home/c/ciaracousins/public_html/doupload.php on line 14

Warning: move_uploaded_file(): Unable to move '/tmp/phpUiHMAz' to '/home/c/ciaracousins/public_html/clothes/baggy.jpg' in /home/c/ciaracousins/public_html/doupload.php on line 14
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.