Jump to content

php photo upload


php_novice2007

Recommended Posts

Hi, I am trying to write a php page which allows the user to upload photos, I want to make a copy of the photo uploaded and store it on the server computer (which is myown for now..)

 

The code I have is this:

 

 

$newName = "I:/Cathy/webpages/images/" . $file_name;

if (!copy($file_name, $newName)) {

print "Error Uploading File.";

exit();

}

 

When I ran the code, I get this warning, and then the copy function didn't work

 

Warning: copy(SA500023.JPG) [function.copy]: failed to open stream: No such file or directory in :\Cathy\webpages\BridgeSite\upload.php on line 59

Error Uploading File.

 

hmm I'm not too sure how to fix this, can someone help? thanks :)

 

Also, I want to be able to create a thumbnail sized image based on the image uploaded, and store it on the server as well.. how do I do this?

 

Thanks~!

Link to comment
Share on other sites

all made

my old one... might still have my stuff

<?php
session_start();

//path from folder of file
$path = "uploads/";



if (!isset($HTTP_POST_FILES['userfile'])){ echo "
<FORM ENCTYPE=multipart/form-data ACTION='' METHOD=POST>

  <p>The file:<br>
    <INPUT TYPE=file NAME=userfile style='border: 1px solid #222222; background-color: #111111; color: #888888'>
  </p>
  <p>
    <INPUT TYPE=submit VALUE=Upload style='border: 1px solid #222222; background-color: #111111; color: #888888'>
  </p>
</FORM>
"; }else{
if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {
if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/png")) {
if (file_exists($path . $HTTP_POST_FILES['userfile']['name'])) { echo "<font color=red>Error</font>! The file already exists, try calling your file a different name.<br><FORM ENCTYPE=multipart/form-data ACTION='' METHOD=POST>

  <p>The file:<br>
    <INPUT TYPE=file NAME=userfile style='border: 1px solid #222222; background-color: #111111; color: #888888'>
  </p>
  <p>
    <INPUT TYPE=submit VALUE=Upload style='border: 1px solid #222222; background-color: #111111; color: #888888'>
  </p>
</FORM>"; }else{
$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$HTTP_POST_FILES['userfile']['name']);
if (!$res) { echo "upload failed!<br>"; exit; } else { echo "upload sucessful<br><br>"; }
echo "File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>";
echo "File Path: <a href=uploads/".$HTTP_POST_FILES['userfile']['name']." TARGET=_BLANK>uploads/".$HTTP_POST_FILES['userfile']['name']."</a><br>";
echo "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>";
echo "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>";
echo "<img src=uploads/".$HTTP_POST_FILES['userfile']['name']."><br>";

}}}}


?>

Link to comment
Share on other sites

Hi,

 

well I wrote up my page based on your code, having it in two seperate pages, and also adding stuff which allows the user to type in a title and any annotations to the photo.

 

For some reason when I try to upload a photo, it will return false on this line and not upload the photo:

 

if (is_uploaded_file($_FILES['file_name']['tmp_name']))

 

The title and annotation files get written except the image is not uploaded :(

 

The code is here, but I've also uploaded them if its hard to read here..

 

upload.html (calls upload.php):

 

<html>
<head>
<title>Upload Photo</title>
</head>
<body>
<H1>Add a photo</H1>
        <form method="post" enctype="multipart/form-data" action=upload.php>

        <table border=0>
<tr><td valign=middle>File to upload (Image):</td>
	<td valign=middle><input type="file" name=file_name size=40></td></tr>
        <tr></tr>
        <tr></tr>
        <tr><td>Title (Optional):</td>
        <td valign=middle><input type="text" size=40 name=title></td></tr>
        <tr></tr>
        <tr></tr>
        <tr><td>Annotations (Optional):</td>
                <td valign=middle><textarea cols=50 rows=5 name=annotation></textarea></td></tr>
        <tr></tr>
        <tr></tr>
        <tr>
                <td><button type=submit name=submit value=upload><font color=darkblue><b>Upload</b></font></button></td>
                <td><button type=submit onclick=self.close()><font color=darkblue><b>Cancel Upload</b></font></button></td>
        </tr>
        </table>
        </form>
Note: only images ending in ".gif", ".pjpeg", ".jpeg", and ".png" are accepted. 
</BODY>
</HTML>

 

upload.php:

<?php
function error1(){
    print "	<html>
	<head>
	<title>Error Page</title>
	</head>
	<body>
	<H1>Error</H1>
	<p>No file specified. Click <a href=upload.html>here</a> to go back and try again.</p>
            </body>
	</html>
	";
}

function error2(){
    print "	<html>
	<head>
	<title>Error Page</title>
	</head>
	<body>
	<H1>Error</H1>
	<p>The chosen file is not an image. Click <a href=upload.html>here</a> to go back and try again.</p>
            </body>
	</html>
   ";
}

function error3(){
    print "
	<html>
	<head>
	<title>Error Page</title>
	</head>
	<body>
	<H1>Error</H1>
	<p>The image already exists. Click <a href=upload.html>here</a> to go back and try again.</p>
            </body>
	</html>
   ";
}

$file_name = $_FILES['file_name']['name'];
$title = $_POST['title'];
$annot = $_POST['annotation'];

$path = "./images/";

print "here 1";
if ($file_name == "") {
print "here 2";
error1();
} else {
print "here 3";
if (is_uploaded_file($_FILES['file_name']['tmp_name'])) {
	print "here";
	if (($_FILES['file_name']['type']=="image/gif") || ($_FILES['file_name']['type']=="image/pjpeg") || ($_FILES['file_name']['type']=="image/jpeg") || ($_FILES['file_name']['type']=="image/png")) {
		if (file_exists($path . $_FILES['file_name']['name'])) { 
			error3();
		} else {
			$res = copy($_FILES['file_name']['tmp_name'], $path .$_FILES['file_name']['name']);
			if (!$res) {
				echo "upload failed!<br>"; 
				exit; 
			} else { 
				echo "upload sucessful<br><br>"; 
			}
			echo "File Name: ".$_FILES['file_name']['name']."<br>";
			echo "File Path: <a href=images/".$_FILES['file_name']['name']." TARGET=_BLANK>images/".$_FILES['file_name']['name']."</a><br>";
			echo "File Size: ".$_FILES['file_name']['size']." bytes<br>";
			echo "File Type: ".$_FILES['file_name']['type']."<br>";
			//echo "<img src=images/".$HTTP_POST_FILES['file_name']['name']."><br>";
		}
	} else {
		//not a image
		error2();
	}
}
list($fileA,$fileB)=split('[.]',$file_name);

print "writing to photoList.txt"; 
$File = "photoList.txt"; 
$Handle = fopen($File, 'a');
$Data = $file_name . "\n"; 
fwrite($Handle, $Data); 
fclose($Handle);

print "writing title";
$handle1 = fopen("./titles/".$fileA.".txt", 'w');
fwrite($handle1,$title);
fclose($handle1);

print "writing annot";
$handle2 = fopen("./annotations/".$fileA.".txt", 'w');
fwrite($handle2,$annot);
fclose($handle2);

}

print "<a href=upload.html>upload another photo</a>";

?>

 

Thanks for any help~!

 

 

[attachment deleted by admin]

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.