Jump to content

Uploading to my own html form


ntesla123

Recommended Posts

This is my attempt to make an upload script to upload images to my own website.

<?php
$ch = curl_init("http://*******.com/imgupload.html");  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            array('file'=>"@/home/user/mypic.jpg",
	      'submit'=>'Submit'));
$postResult = curl_exec($ch);
curl_close($ch);
print "$postResult";
?>

The working upload form I'm trying to post to:

<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html> 

 

Why doesn't this work?

Link to comment
Share on other sites

Chances are, the photos ARE being uploaded.  Just to your tmp file location.  I'll show you my process.

 

I have a very buggy, not yet safe upload form that I've written during the design phase of my site, but it does indeed work.  You're free to use it, and I'll describe what it does along the way.

 

upload.php

<?php
include('header.php');echo
'<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data"><br />
<label for="file">toast:</label><br />
<input type="file" name="upload" /><br />
        <input type ="text" name ="name" /><br />
<input type="submit" name="submit" value="Upload File" />
</form>';
?>

 

assigned variables from my all-inclusive script (header.php)

//Defines the directory for file uploads
$avatar_dir = 'avatars/';
//Since files are uploaded with a standard definitions, let's break it down to easy calls
$tmp_name = $_FILES['upload']["tmp_name"];
$avatarftype = $_FILES['upload']["type"];
//explode is needed to separate "image/jpg or png or gif" into just /jpg or /png or /gif..  Then replace the slash w/ a dot
$fext = explode("image", $avatarftype);
$fext = str_replace("/", ".", $fext);
//this is the result of the extension strip and replace
$ext = $fext[1];
//The name you've chosen, plus it's extension
$name = ($_POST['name'];
$mypic = ($name.$ext);

 

the processing script (upload_file.php):

<?php
include('header.php');
// Determine file types and size. This allows for only gifs, jpgs (both IE and other browser safe) and pngs under 60 kb
if ((($_FILES['upload']["type"] == "image/gif")
|| ($_FILES['upload']["type"] == "image/jpeg")
|| ($_FILES['upload']["type"] == "image/pjpeg")
|| ($_FILES['upload']["type"] == "image/png"))
&& ($_FILES['upload']["size"] < 60000)){

//Notify if error
if ($_FILES['upload']["error"] > 0){
    	echo "Return Code: " . $_FILES[$name]["error"] . "<br />";
}else{

// Upload it to server and log into database
echo $mypic;
    move_uploaded_file($tmp_name, $avatar_dir.$mypic);
//if you chose to store it in the database
    mysql_query("INSERT INTO 'yourtable' (file) VALUE (($mypic);
}    
//Kick out
}else{
  echo 'Invalid file,';
   }

?> 

 

I heavily modified my own personal scripts to provide this to you. I am pretty sure it works, but it's untested.  The intent here is to help you learn while giving you a living example.

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.