Page 1 of 1

trying to save files to server via a form

Posted: Thu Feb 28, 2008 2:32 pm
by bobafart
i am trying to build a form where the user inputs an image from a weblink or an upload.

the form code is:

Code: Select all

 
<form action="" method="post">
    <p>You can either upload an icon or you can point us to a weblink.  The jpeg, gif or png file will be resized to 25 x 25 pixels.</p>
    <p>Weblink Pixel Icon: <input type="text" name="pixelURL" size="55" value="http://"></p>
    <p>OR</p>
  <p>Upload Pixel Icon: <input type="hidden" name="MAX_FILE_SIZE" value="100000" /><input type="file" name="uploadpixelURL" size="44"></p>
    <p>Step 2:</p>
    <p>Now all you have to do is enter the web link you want the traffic to go to:</p>                                                                                                                                  
    <p>Destination: <input type="text" name="destinationURL" size="55" value="http://"></p>
    <p><input type="image" name="confirmPixelBuy" src="http://www.foo.com/img/icons/icon_claimmypixels.gif"></p>
</form>
 

Code: Select all

 
if(isset($_POST['uploadpixelURL'])){
        // user decided to upload a pixel using var $uploadpixelURL
        if($_FILES['uploadpixelURL']['type']=='image/jpeg'){
            $fileext = '.jpg';
    }else{
        $fileext = '.gif';
    }
    $newname = $_SESSION[userid].'_'.md5(rand(1,1000)).$fileext;
        if(move_uploaded_file($_FILES['uploadpixelURL']['tmp_name'], $target_path)) {
            chmod($target_path,0777);
    }else{
        echo "<p>There was an error uploading the file, please try again</p>";
    }
    copy($pixelURL, $target_path);
    $filename = $_FILES['uploadpixelURL']['tmp_name'];
        echo '<p>filename is:__ '.$filename.'</p>';
       rename($filename, $newname);
    $pixelURL = 'http://www.foo.com/img/pixelate/'.$newname;
    echo '<p>pixelURL is:__ '.$pixelURL.'</p>';
}else{
       // user decided to save a weblink for the pixel, so save it locally to server
    copy($pixelURL, $target_path);
}
 

if i try to upload a file the error i get is:

Code: Select all

 
pixelURL is: http://
 
uploadpixelURL is: coke.gif
 
destinationURL is: http://www.coca-cola.com/glp/d/index.html
 
There was an error uploading the file, please try again
 
Warning: copy(http://) [function.copy]: failed to open stream: Success in /home/www/promotion/pixelate/add.php on line 106
 
filename is:__
 
Warning: rename(,1_35f4a8d465e6e1edc05f3d8ab658c551.gif) [function.rename]: No such file or directory in /home/www/promotion/pixelate/add.php on line 109
 
pixelURL is:__ http://www.foo.com/img/pixelate/1_35f4a ... 58c551.gif
 

Re: trying to save files to server via a form

Posted: Thu Feb 28, 2008 10:21 pm
by hawkenterprises
couple of things

<FORM ACTION="http://server.dom/cgi/handle"
ENCTYPE="multipart/form-data"
METHOD=POST>

You have to have enctype for uploads to work.


Secondly check your file permssions, generally chmod 775 or 777