Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image Upload

Status
Not open for further replies.

TheSponge

Technical User
Jul 2, 2003
442
GB
I want to echo back the filename and filepath once an image has been uploaded to the server?

The image upload works fine, and it echoes back "upload succesful"

can someone help? here is the bottom of my script:

// What is the files temporary name?
$file = $_FILES['userfile']['tmp_name'];

// What is the files actual name?
$filename = $_FILES['userfile']['name'];

// Does this file already exist on the server?
if(file_exists($uploaddir . $filename)) {
die("A file with that name already exists on this server.");
} else {
// This file does not already exist, so copy it.
copy($file, $uploaddir.$filename) or die("Could not copy file.");
}

// All done! :)
echo "Upload successful";
}


A+,CCNA
 
YES, I want to see "upload succesful" (which it does)

but I also want the script to echo back the filename and path of the file that was just uploaded..

I dont know the code...

echo filename$ . $filepath. (something like that)

A+,CCNA
 
I have no idea, thats why Im asking........

A+,CCNA
 
I'm not sure what it is you want to output.

Do you want to output information about where PHP temporarily stored the file? Take a look in $_FILES and output what you need using print statements.

Do you want to output to where your script has moved the files to? The your scripts own $uploaddir and $filename would have the information.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I know where the uploaded files go...

mysite.co.uk/uploads/

then whatever file has been uploaded, this script is for a phpbb forum, and I want users to upload a file to the server, then copy and paste the filepath..

i.e
thefile.jpg


have a look at
he has one there...



A+,CCNA
 
no that didnt work, see the link above, I gave you..

thanks

A+,CCNA
 
What, exactly, is the script not doing?

I uploaded a graphic I downloaded from Google, the script accepted it, and provided a URL. When I pasted the URL into a new browser window, the browser displayed the graphic.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
yes thats what I want on my script....

see my script I have provided at the top....

A+,CCNA
 
1. PHP is case sensitive when it comes to variables. $_Files and $_FILES is thus not the same. When sleipnir214 said $_FILES, he meant the $_FILES array (similar to $_POST and $_GET). Check Handling file uploads in the manual.

2. Variables in single-quotes are printed as variables instead of being parsed. Rather than just putting a variable in quotes like a string concate the variable at the end:
Code:
print '[URL unfurl="true"]http://www.mysite.com/uploads/'[/URL] . $_FILES['userfile']['name'];
Read more about how quotes relate to your output here.
 
Im pretty much new to PHP,

so, is the above code you printed,

what I need?

here is the full script I have:
<?php

/********************************\
*
* Image Upload Script
*
* By: Musicalmidget
*
\********************************/

// Create the main upload function
function do_upload() {

// Create an array containing all valid upload file types for this script
$allowed_types = array(
"image/gif" => "gif",
"image/pjpeg" => "jpg",
// Add more types here if you like
);

// Check to see if the file type is in the allowed types array
if(!array_key_exists($_FILES['userfile']['type'], $allowed_types)) {
die("Invalid file type.");
}

// Set the maximum uploadable file size => 51200 = 50kb
$maxfilesize = 51200;

// Is the file larger than it is allowed to be?
if($_FILES['userfile']['size'] > $maxfilesize) {
die("File too large");
}

// Where will the file be uploaded to?
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . "/uploads/";

// What is the files temporary name?
$file = $_FILES['userfile']['tmp_name'];

// What is the files actual name?
$filename = $_FILES['userfile']['name'];

// Does this file already exist on the server?
if(file_exists($uploaddir . $filename)) {
die("A file with that name already exists on this server.");
} else {
// This file does not already exist, so copy it.
copy($file, $uploaddir.$filename) or die("Could not copy file.");
}

// All done! :)
echo "Upload successful";
}
print'







?>
<html>
<head>
<title>Websponge Image Upload</title>
</head>
<body bgcolor="#dddddd">
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="do_upload">
<input type="file" name="userfile"><br />
<input type="submit" name="submit" value="Upload Image">
</form>
</body>
</html>
<?php
// If the form has been completed, execute the upload function (above).
if($_POST['action'] == "do_upload") {
do_upload();
}
?>


A+,CCNA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top