Hi,

I'm trying to create an upload utility in my own shopping cart software that allows the store owner to upload buttons for the cart system. For each button I have made a default .png image and store it in a folder at root/images/buttons. I want whatever png image is uploaded to be renamed to a specific name like go.png. So they could upload any image thats a png and it will automatically rename it to go.png. This will overwrite the current go.png in the folder and their button will be updated. I do not want to do a database storage of images becuase the database is already 61 tables and I need to cut down on the size of things and just make it simple. Here's the code I have so far. but I am getting the error:

Warning: rename($file_name,go.png) [function.rename]: No such file or directory in /home/papermom/public_html/manager/settings/buttons2.php

<?php


// Your file name you are uploading
$file_name = $HTTP_POST_FILES['ufile']['name'];


rename( '$file_name', 'go.png' );

$path= "../images/buttons/";
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "Successful<BR/>";

echo "File Name :".$new_file_name."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";
}
else
{
echo "Error";
}
}

include("../includes/header.inc.php");
?>

<p class="pageTitle">Customize Store Buttons</p>
<?php
if(isset($msg)){ 
	echo stripslashes($msg); 
} else { 
?>
<p class="copyText">Edit Buttons</p>
<?php } ?>


<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="buttons2.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td><strong>Single File Upload </strong></td>
</tr>
<tr>
<td>Select file
<input name="ufile" type="file" id="ufile" size="50" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="Submit" value="Upload" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>



<?php include("../includes/footer.inc.php"); ?>

    php wont parse variables in single quotes:

    rename( '$file_name', 'go.png' );
    

    try:

    rename( $file_name, 'go.png' );
    

      Did that change and now I get this error

      Warning: rename(,go.png) [function.rename]: Inappropriate ioctl for device in /home/papermom/public_html/manager/settings/buttons2.php on line 29

        That means that $file_name is empty. I dont see any checks in your script that file is actually uploaded.

        And use $_FILES instead of $HTTP_POST_FILES. Long version has been deprecated for years already. Also use [man]move_uploaded_file[/man] instead of copy.

          What if I changed my php to:

          <?php
          if (array_key_exists('upload', $_POST))
          {
          //define constant for upload folder
          define('UPLOAD_DIR', 'images/buttons/');
          
          //replace any spaces in original filename with underscores
          //at the same time, assign to a simpler variable
          $file ='go.png';
          
          //move the file to the upload folder and rename it
          move_uploaded_file($_FILES['somethinggoesherenotsurewhat']['tmp_name'], UPLOAD_DIR.$file);
          }
          ?> 
          

            Ok I changed the php to this

            if (array_key_exists('upload', $_POST))
            {
            //define constant for upload folder
            define('UPLOAD_DIR', 'images/buttons/');
            
            //replace any spaces in original filename with underscores
            //at the same time, assign to a simpler variable
            $file ='go.png';
            
            //move the file to the upload folder and rename it
            move_uploaded_file($_FILES['upload']['tmp_name'], UPLOAD_DIR.$file);
            }
            

            And there are no errors...but there is also nothings in my upload directory lol...I think I'll also need it to confirm the size and file type so that they dont try to upload a go.gif ...that wont work...

              You can check straight using $FILES:

              if (isset($_FILES['ufile']['tmp_name']))
              {
                  ...
              

                Ok now what I have don instead of posting echo's for success...I echoed the image itself so I can see right away if it was overwritten with x.gif. No luck arg....so frustrating!! lol

                <?php
                if (isset($_FILES['ufile']['tmp_name']))
                { 
                $UPLOAD_DIR ='/images/buttons/';
                $file ='go.gif';
                move_uploaded_file($_FILES['ufile']['tmp_name'], UPLOAD_DIR.$file);
                }
                ?>
                
                
                <table width="530" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
                <tr>
                <form action="buttons2.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
                <td>
                <table width="100&#37;" border="0" cellpadding="10" cellspacing="1" bgcolor="#FFFFFF">
                <tr>
                <td width="20%"><strong>Go Button </strong></td>
                <td width="50%"><input name="ufile" type="file" id="ufile" size="25" /></td>
                <td width="13%"><input type="submit" name="Submit" value="Upload" /></td>
                <td width="17%"><div align="center">
                  <?php
                echo "<img src=\"/images/buttons/go.gif\" title=\"Go Button\" alt=\"Go Button\" />"; 
                ?>
                </div></td>
                </tr>
                </table>
                </td>
                </form>
                </tr>
                </table>
                

                Boy that php sure does like a little skimpy.... Does anyone have any ideas?

                  Check what's in the $FILES array after upload:

                  print_r($_FILES);
                  

                  There is $_FILES['ufile']['error']. If its anything else than 0 then there was problem uploading it.
                  Error codes can be found here

                  Ok.. I spotted the problem:

                  move_uploaded_file($_FILES['ufile']['tmp_name'], UPLOAD_DIR.$file);
                  

                  There is $ sign missing in UPLOAD_DIR so it should be $UPLOAD_DIR. In earlier script you defined it but in the latest you assigned it to a variable.

                    I'll do this right away...ill report back in a minute 🙂 yay!

                      Warning: move_uploaded_file(/images/buttons/go.gif) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/papermom/public_html/manager/settings/buttons2.php on line 36

                      Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpKgiw9D' to '/images/buttons/go.gif' in /home/papermom/public_html/manager/settings/buttons2.php on line 36
                      Array ( [ufile] => Array ( [name] => ad2.gif [type] => image/gif [tmp_name] => /tmp/phpKgiw9D [error] => 0 [size] => 20020 ) )

                      ----this shows above my little upload box html....here is the code i have now:

                      <?php
                      if (isset($_FILES['ufile']['tmp_name']))
                      { 
                      $UPLOAD_DIR ='/images/buttons/';
                      $file ='go.gif';
                      move_uploaded_file($_FILES['ufile']['tmp_name'], $UPLOAD_DIR.$file);
                      }
                      print_r($_FILES); 
                      ?>
                      
                      
                      <table width="530" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
                      <tr>
                      <form action="buttons2.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
                      <td>
                      <table width="100&#37;" border="0" cellpadding="10" cellspacing="1" bgcolor="#FFFFFF">
                      <tr>
                      <td width="20%"><strong>Go Button </strong></td>
                      <td width="50%"><input name="ufile" type="file" id="ufile" size="25" /></td>
                      <td width="13%"><input type="submit" name="Submit" value="Upload" /></td>
                      <td width="17%"><div align="center">
                        <?php
                      echo "<img src=\"/images/buttons/go.gif\" title=\"Go Button\" alt=\"Go Button\" />"; 
                      ?>
                      </div></td>
                      </tr>
                      </table>
                      </td>
                      </form>
                      </tr>
                      </table>
                      

                        Remember that when you set the upload dir and use /images/buttons/ it is absolute to the server. So if using absolutepath you should use something like:

                        $UPLOAD_DIR ='/home/papermom/public_html/images/buttons/';
                        

                        ..or use relative path to your script eg "../../images/buttons"

                          sweet that did it!!! It all works perfect now it the simlified form!! Now I just need to make it only accept png's lol Thank you Cahva!!

                            and you can do that by verifying the files' mime type... google or search this forum and youll find some reusable code for doing that

                              ok...question... How is it that on the server in the images/buttons directory...my test go.gif file is the same however on the php page I made where it echos the image in the html form like

                                <?php
                              echo "<img src=\"/images/buttons/go.gif\" title=\"Go Button\" alt=\"Go Button\" />"; 
                              ?>
                              

                              It has the new image I uploaded? How can that be? Is it showing from the tmp?

                                also another important thing to mention...my test go.gif image is 1.07 kb and when i upload the new x.gif image to be renamed to go.gif and replace that image...it changed the kb to 19.55kb for example. However when i view the image from file manager on cpanel....its the same image.

                                  nevermind....probably a problem with file manager in cpanel becuase the template for the website calls for that same button and voila..its my new uploaded button!

                                  When I get the file type restriction I will post a final reply and close the thread and resolved. I hope that someone else will find it helpful when wanting to do the same thing! Sometimes its really hard to find some code that is xactly what you need 🙂 Thanks to everyone so very much for the hard work in helping me solve this 🙂 I have learned a ton!!

                                    yep it shows up. I think becuase i've had my cpanel and file manager open straight for days and days working on my new cart system...it gets 'stuck' lol.

                                      Write a Reply...