/    Sign up×
Community /Pin to ProfileBookmark

PHP Upload Script

I have been following a video tutorial on how to make an image upload website, but he hasn’t finished it yet and I went to get it done tonight.
index.php code:

[code=php]<html>
<form action=”upload.php” method=”post” enctype=”multipart/form-data”>
<input type=”file” name=”myfile”><p>
<input type=”submit” value=”Upload”>
</form>

</html>[/code]

upload.php code:

[code=php]<?php

// properties of the uploaded file
$name = $_FILES[“myfile”][“name”];
$type = $_FILES[“myfile”][“type”];
$size = $_FILES[“myfile”][“size”];
$temp = $_FILES[“myfile”][“tmp_name”];
$error = $_FILES[“myfile”][“error”];

if ($error > 0)
die(“Error uploading file! Code $error.”);
else
{

if($type = “video/avi”) //conditions for the file
{
die(“That format is not allowed”);
}
else
{
move_uploaded_file($temp,”uploaded/”.$name);
echo “Upload complete”;
}
}

?>[/code]

At the moment the file you upload goes into the uploaded folder, but it does not show the user who uploaded the file the image.

You can view a live example here:
[url]http://cure2boredom.x10hosting.com/SpeedyUpload/[/url]

Thanks for the help

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@jassincNov 22.2009 — Try this...

[code=php]
// properties of the uploaded file
$name = $_FILES["myfile"]["name"];
$type = $_FILES["myfile"]["type"];
$size = $_FILES["myfile"]["size"];
$temp = $_FILES["myfile"]["tmp_name"];
$error = $_FILES["myfile"]["error"];

if ($error > 0) {
die("Error uploading file! Code $error.");
} else {

if($type = "video/avi") {
die("That format is not allowed");

} else {
if(move_uploaded_file($temp,"uploaded/".$name)) {
echo "Upload complete";
} else {
echo "Failed to complete the upload";
}
}

}
[/code]


Hopefully that solves the issue for you.
Copy linkTweet thisAlerts:
@Nate1n22authorNov 22.2009 — Now it is saying That format is not allowed, which I am uploading a .png

Last night it wasn't doing that
Copy linkTweet thisAlerts:
@criterion9Nov 22.2009 — 
if($type = "video/avi") {
[/quote]

Should be:
[code=php]
if($type == "video/avi") {
[/code]
Copy linkTweet thisAlerts:
@jassincNov 22.2009 — Remove the type checker so you can upload anything as follows

[code=php]
// properties of the uploaded file
$name = $_FILES["myfile"]["name"];
$type = $_FILES["myfile"]["type"];
$size = $_FILES["myfile"]["size"];
$temp = $_FILES["myfile"]["tmp_name"];
$error = $_FILES["myfile"]["error"];

if ($error > 0) {
die("Error uploading file! Code $error.");
} else {

if(move_uploaded_file($temp,"uploaded/".$name)) {
echo "Upload complete";
} else {
echo "Failed to complete the upload";
}
}

[/code]
Copy linkTweet thisAlerts:
@Nate1n22authorNov 22.2009 — Remove the type checker so you can upload anything as follows

[code=php]
// properties of the uploaded file
$name = $_FILES["myfile"]["name"];
$type = $_FILES["myfile"]["type"];
$size = $_FILES["myfile"]["size"];
$temp = $_FILES["myfile"]["tmp_name"];
$error = $_FILES["myfile"]["error"];

if ($error > 0) {
die("Error uploading file! Code $error.");
} else {

if(move_uploaded_file($temp,"uploaded/".$name)) {
echo "Upload complete";
} else {
echo "Failed to complete the upload";
}
}

[/code]
[/QUOTE]

used that and it fixed the problem, but it still doesnt show the uploader the picture, it just shows "Upload Complete" which is should, but I want the image also. It shows the image in the "uploaded" folder when I view it in FileZilla.

live example:

http://cure2boredom.x10hosting.com/SpeedyUpload/upload.php


Thanks
Copy linkTweet thisAlerts:
@jassincNov 22.2009 — You'll need this...

[code=php]
// properties of the uploaded file
$name = $_FILES["myfile"]["name"];
$type = $_FILES["myfile"]["type"];
$size = $_FILES["myfile"]["size"];
$temp = $_FILES["myfile"]["tmp_name"];
$error = $_FILES["myfile"]["error"];

if ($error > 0) {
die("Error uploading file! Code $error.");
} else {

if(move_uploaded_file($temp,"uploaded/".$name)) {
?>
<!-- Start Success HTML -->
Upload complete <br/><br/>
<img src="uploaded/<? echo $name; ?>" alt="Uploaded Image">
<!-- End Success HTML -->
<?
} else {
echo "Failed to complete the upload";
}
}
[/code]


You can strip out and HTML you don't want (or add some) in between where I've commented it.
Copy linkTweet thisAlerts:
@Nate1n22authorNov 23.2009 — It works now, and the image uploads, but I would like it to upload the image to a randomized URL that someone can copy and paste.
Copy linkTweet thisAlerts:
@Nate1n22authorNov 23.2009 — Sorry for the double post, but I would like it like this

http://www.picrange.com/images/klz1259014403d.JPG

The klz1259014403d.JPG being the image
Copy linkTweet thisAlerts:
@jassincNov 23.2009 — [code=php]<?
// properties of the uploaded file
$name = $_FILES["myfile"]["name"];
$type = $_FILES["myfile"]["type"];
$size = $_FILES["myfile"]["size"];
$temp = $_FILES["myfile"]["tmp_name"];
$error = $_FILES["myfile"]["error"];

if ($error > 0) {
die("Error uploading file! Code $error.");
} else {

$ext = strtolower(substr($name, strrpos($name, "."))); // Get Extension
$newname = time().rand(100,999).$ext; // Generate Random File Name
if(move_uploaded_file($temp,"uploaded/".$newname)) {
?>
<!-- Start Success HTML -->
Upload complete <br/><br/>
<img src="uploaded/<? echo $newname; ?>" alt="Uploaded Image">
<!-- End Success HTML -->
<?
} else {
echo "Failed to complete the upload";
}
}
?>[/code]
Copy linkTweet thisAlerts:
@Nate1n22authorNov 24.2009 — [code=php]<?
// properties of the uploaded file
$name = $_FILES["myfile"]["name"];
$type = $_FILES["myfile"]["type"];
$size = $_FILES["myfile"]["size"];
$temp = $_FILES["myfile"]["tmp_name"];
$error = $_FILES["myfile"]["error"];

if ($error > 0) {
die("Error uploading file! Code $error.");
} else {

$ext = strtolower(substr($name, strrpos($name, "."))); // Get Extension
$newname = time().rand(100,999).$ext; // Generate Random File Name
if(move_uploaded_file($temp,"uploaded/".$newname)) {
?>
<!-- Start Success HTML -->
Upload complete <br/><br/>
<img src="uploaded/<? echo $newname; ?>" alt="Uploaded Image">
<!-- End Success HTML -->
<?
} else {
echo "Failed to complete the upload";
}
}
?>[/code]
[/QUOTE]

That didn't generate the random URL.

Upload.php code below:

[code=php]<?php
// properties of the uploaded file
$name = $_FILES["myfile"]["name"];
$type = $_FILES["myfile"]["type"];
$size = $_FILES["myfile"]["size"];
$temp = $_FILES["myfile"]["tmp_name"];
$error = $_FILES["myfile"]["error"];

if ($error > 0) {
die("Error uploading file! Code $error.");
} else {

$ext = strtolower(substr($name, strrpos($name, "."))); // Get Extension
$newname = time().rand(100,999).$ext; // Generate Random File Name
if(move_uploaded_file($temp,"uploaded/".$newname)) {
?>
<!-- Start Success HTML -->
Upload complete <br/><br/>
<img src="uploaded/<? echo $newname; ?>" alt="Uploaded Image">
<!-- End Success HTML -->
<?
} else {
echo "Failed to complete the upload";
}
}
?>[/code]
Copy linkTweet thisAlerts:
@jassincNov 24.2009 — I'd strongly suggest you learn the basics of PHP so you understand what is happening and can modify the script accordingly.

Go to Tizag.com and run through the php tutorials as you really should be able to read the code and have it read out the address. If you can't do that then you shouldn't be delving into PHP.
Copy linkTweet thisAlerts:
@Nate1n22authorNov 24.2009 — [code=php]<?
// properties of the uploaded file
$name = $_FILES["myfile"]["name"];
$type = $_FILES["myfile"]["type"];
$size = $_FILES["myfile"]["size"];
$temp = $_FILES["myfile"]["tmp_name"];
$error = $_FILES["myfile"]["error"];

if ($error > 0) {
die("Error uploading file! Code $error.");
} else {

$ext = strtolower(substr($name, strrpos($name, "."))); // Get Extension
$newname = time().rand(100,999).$ext; // Generate Random File Name
if(move_uploaded_file($temp,"uploaded/".$newname)) {
?>
<!-- Start Success HTML -->
Upload complete <br/><br/>
<img src="uploaded/<? echo $newname; ?>" alt="Uploaded Image">
<!-- End Success HTML -->
<?
} else {
echo "Failed to complete the upload";
}
}
?>[/code]
[/QUOTE]


I'd strongly suggest you learn the basics of PHP so you understand what is happening and can modify the script accordingly.

Go to Tizag.com and run through the php tutorials as you really should be able to read the code and have it read out the address. If you can't do that then you shouldn't be delving into PHP.[/QUOTE]


Thanks but I got it to work, it was something I didn't get but with a little tweaking it works how I want it to now.

Thanks all for the help
Copy linkTweet thisAlerts:
@barnabaApr 15.2010 — I'm using tutorials too. Especially php tutorials.
×

Success!

Help @Nate1n22 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...