/    Sign up×
Community /Pin to ProfileBookmark

I want an upload form similar to imageshack.us, just where you can upload any file, and it generates a random URL (so more than one name don’t appear several times), and displays the URL when the upload is complete.
and maybe a progress bar.

I am a noob to this, so please elaborate, just so I know what your saying.

I’m not for sure if this should be completely done in php or something else, so sorry if I posted in the wrong section.

Please help!!!

thanks.

to post a comment
PHP

31 Comments(s)

Copy linkTweet thisAlerts:
@hastxSep 20.2007 — There is a recent thread that goes over the basics of file uploads

here.

To get a random URL all you have to do is append the file name with a random number using the rand() function. see here.
Copy linkTweet thisAlerts:
@steventowtauthorSep 20.2007 — thanks.

but problem,

I get this:

Warning: move_uploaded_file(): open_basedir restriction in effect. File(/home/public_html/selifx/uploads/1476867221_l.jpg) is not within the allowed path(s): (/home/stevento/:/usr/lib/php:/usr/local/lib/php:/tmp:/var/tmp:/home/vdeck/tmp/:/usr/local/bin/mogrify:/usr/local/bin/convert:/usr/sbin/sendmail) in /home/stevento/public_html/selifx/upload.php on line 10

upload failed! Here is some more info on your upload:Array ( [filename] => Array ( [name] => 1476867221_l.jpg [type] => image/jpeg [tmp_name] => /var/tmp/phpztf3d4 [error] => 0 [size] => 18944 ) )

when trying to upload a file through that form.

any help on that???

thanks.
Copy linkTweet thisAlerts:
@steventowtauthorSep 20.2007 — what does that mean???
Copy linkTweet thisAlerts:
@hastxSep 20.2007 — Your home dir is "/home/stevento/....." and you are trying to upload to "/home/public_html/...".

You have to have write access to the directory you are trying to upload to.
Copy linkTweet thisAlerts:
@steventowtauthorSep 20.2007 — I'm sorry, but I'm new to this, and have another problem,

Warning: move_uploaded_file(/home/stevento/public_html/selifx/uploads/1476867221_l.jpg): failed to open stream: Permission denied in /home/stevento/public_html/selifx/upload.php on line 10

Warning: move_uploaded_file(): Unable to move '/var/tmp/phpTGp2jW' to '/home/stevento/public_html/selifx/uploads/1476867221_l.jpg' in /home/stevento/public_html/selifx/upload.php on line 10

upload failed! Here is some more info on your upload:Array ( [filename] => Array ( [name] => 1476867221_l.jpg [type] => image/jpeg [tmp_name] => /var/tmp/phpTGp2jW [error] => 0 [size] => 18944 ) )

what's that mean???
Copy linkTweet thisAlerts:
@TJ111Sep 20.2007 — It means that an anonymous user doesn't have access to write to that directory. If you have shell access you can do the following:
<i>
</i>chmod a=rwx /home/stevento/public_html/selfix/
Copy linkTweet thisAlerts:
@steventowtauthorSep 20.2007 — where exactly do I put that???


what is shell access???
Copy linkTweet thisAlerts:
@TJ111Sep 20.2007 — Shell (SSH) is basically command line access to the server hosting your website. If you don't know what it is or how to use it, you may want to ask your web-host to do it for you.

You could also consider using a database instead.
Copy linkTweet thisAlerts:
@steventowtauthorSep 20.2007 — can you tell me how to use a database for this???
Copy linkTweet thisAlerts:
@TJ111Sep 21.2007 — It's easy to do. Give [url=http://www.php-mysql-tutorial.com/php-mysql-upload.php]this tutorial[/url] a try. Get some code together and let us know how it goes.
Copy linkTweet thisAlerts:
@steventowtauthorSep 22.2007 — I'm not gonna try the database one yet.

this is the .php file I'm using:
<i>
</i>&lt;?
$filename = $_POST['filename']; //this is the filename input of your form
$uploaddir = '/home/stevento/public_html/selfix/uploads/temp/'; //where you want the file to go
$file = basename($_FILES['filename']['name']);
$file = stripslashes($file);

$uploadfile = $uploaddir . $file;

//the move_uploaded_file() function is what moves the temp working file to it's final location.
if (move_uploaded_file($_FILES['filename']['/home/stevento/public_html/selfix/uploads/'], $uploadfile)) {
echo "&lt;pre&gt;";
echo "&lt;span style="color:green;font-weight:bold;"&gt;Successfully Uploaded: $file&lt;/span&gt;...n";
} else {
echo "&lt;span style="color:#ad0000;font-weight:bold;"&gt;upload failed!&lt;/span&gt;n";
}
echo 'Here is some more info on your upload:';
print_r($_FILES);
echo "&lt;/pre&gt;";
?&gt;


and I keep getting this error:
<i>
</i>upload failed! Here is some more info on your upload:Array ( [filename] =&gt; Array ( [name] =&gt; l_47eee52282beb800613b4bd977e01ba3.jpg [type] =&gt; image/jpeg [tmp_name] =&gt; /var/tmp/phppty8BE [error] =&gt; 0 [size] =&gt; 43232 ) )
Copy linkTweet thisAlerts:
@steventowtauthorSep 23.2007 — okay, My host, says that I don't have SSH access with my plan, so how would I go about doing this without SSH????
Copy linkTweet thisAlerts:
@hastxSep 23.2007 — change
[code=php]
if (move_uploaded_file($_FILES['filename']['/home/stevento/public_html/selfix/uploads/'], $uploadfile)) {
[/code]


to
[code=php]
if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile)) {
[/code]


Your trying to move the completed file to the upload destination, which is the same thing...you need to move PHP's [B]temp working file[/B] to the upload destination.

tmp_name is an element in the files array. It is assigned a random name by php so your script can work with the upload, before moving it to the final dest.
Copy linkTweet thisAlerts:
@steventowtauthorSep 23.2007 — that's still not gonna work cause' I don't SSH access.
Copy linkTweet thisAlerts:
@hastxSep 23.2007 — well SSH isn't going to help, if you don't have the right code.

If you fix the code you might get lucky and not have to set permission.
Copy linkTweet thisAlerts:
@NogDogSep 23.2007 — If you have FTP access to your site, most FTP clients provide a means to change the access on files/directories. Typically you would do something like right-click the icon for the directory in question, select the option for something like "view/change permissions", and set it to read/write/execute access for all ("777" in UNIX/Linux systems).
Copy linkTweet thisAlerts:
@steventowtauthorSep 23.2007 — well SSH isn't going to help, if you don't have the right code.

If you fix the code you might get lucky and not have to set permission.[/QUOTE]


Can you help me out to get this working???
Copy linkTweet thisAlerts:
@hastxSep 24.2007 — At the top of your script put the line in
[code=php]
error_reporting(E_ALL);
[/code]

Then fix the line that is wrong.

Post back the errors you get when you try uploading. If permissions are the issue after that then we can deal with it.

Like NogDog said, some FTP clients will allow you to change permission, and some hosts have control panels to do this also.
Copy linkTweet thisAlerts:
@steventowtauthorSep 24.2007 — now I get this:
<i>
</i>Notice: Undefined index: filename in /home/stevento/public_html/selifx/upload.php on line 3

Warning: move_uploaded_file(/home/stevento/public_html/selfix/uploads/preview.jpg): failed to open stream: No such file or directory in /home/stevento/public_html/selifx/upload.php on line 11

Warning: move_uploaded_file(): Unable to move '/var/tmp/phpUB91dD' to '/home/stevento/public_html/selfix/uploads/preview.jpg' in /home/stevento/public_html/selifx/upload.php on line 11
upload failed! Here is some more info on your upload:Array ( [filename] =&gt; Array ( [name] =&gt; preview.jpg [type] =&gt; image/jpeg [tmp_name] =&gt; /var/tmp/phpUB91dD [error] =&gt; 0 [size] =&gt; 495656 ) )


using this php:
<i>
</i>&lt;?
error_reporting(E_ALL);
$filename = $_POST['filename']; //this is the filename input of your form
$uploaddir = '/home/stevento/public_html/selfix/uploads/'; //where you want the file to go
$file = basename($_FILES['filename']['name']);
$file = stripslashes($file);

$uploadfile = $uploaddir . $file;

//the move_uploaded_file() function is what moves the temp working file to it's final location.
if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile)) {
echo "&lt;pre&gt;";
echo "&lt;span style="color:green;font-weight:bold;"&gt;Successfully Uploaded: $file&lt;/span&gt;...n";
} else {
echo "&lt;span style="color:#ad0000;font-weight:bold;"&gt;upload failed!&lt;/span&gt;n";
}
echo 'Here is some more info on your upload:';
print_r($_FILES);
echo "&lt;/pre&gt;";
?&gt;
Copy linkTweet thisAlerts:
@hastxSep 24.2007 — The notice doesn't have any effect in the functionality, but it is being caused by the line:
[code=php]
$filename = $_POST['filename']; //this is the filename input of your form
[/code]

If you comment out that line the notice will go away.

It does look like permissions are the issue now. You can see in the files array that the files name was preview.jpg, and was assigned the tmp_name 'phpUB91dD', no errors found, and has a size of 495656. The warnings are being generated while trying to transfer the file to the final location.

What type of FTP Client do you use? With filezilla you should be able to right-click on the upload directory and get to the permission values.
Copy linkTweet thisAlerts:
@hastxSep 24.2007 — See the attached image for instructions on how to set permissions using filzilla.

[upl-file uuid=787f9702-d7ca-4766-ade1-3c6a2a75c580 size=94kB]file attributes1.jpg[/upl-file]
Copy linkTweet thisAlerts:
@steventowtauthorSep 24.2007 — okay. I chose write for all permissions in that directory.

I get this when trying to upload:
<i>
</i>Notice: Undefined index: filename in /home/stevento/public_html/selifx/upload.php on line 3

Warning: move_uploaded_file(/home/stevento/public_html/selfix/uploads/preview.jpg): failed to open stream: No such file or directory in /home/stevento/public_html/selifx/upload.php on line 11

Warning: move_uploaded_file(): Unable to move '/var/tmp/php5JdXsT' to '/home/stevento/public_html/selfix/uploads/preview.jpg' in /home/stevento/public_html/selifx/upload.php on line 11
upload failed! Here is some more info on your upload:Array ( [filename] =&gt; Array ( [name] =&gt; preview.jpg [type] =&gt; image/jpeg [tmp_name] =&gt; /var/tmp/php5JdXsT [error] =&gt; 0 [size] =&gt; 495656 ) )


php:
<i>
</i>&lt;?
error_reporting(E_ALL);
$filename = $_POST['filename'];
$uploaddir = '/home/stevento/public_html/selfix/uploads/'; //where you want the file to go
$file = basename($_FILES['filename']['name']);
$file = stripslashes($file);

$uploadfile = $uploaddir . $file;

//the move_uploaded_file() function is what moves the temp working file to it's final location.
if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile)) {
echo "&lt;pre&gt;";
echo "&lt;span style="color:green;font-weight:bold;"&gt;Successfully Uploaded: $file&lt;/span&gt;...n";
} else {
echo "&lt;span style="color:#ad0000;font-weight:bold;"&gt;upload failed!&lt;/span&gt;n";
}
echo 'Here is some more info on your upload:';
print_r($_FILES);
echo "&lt;/pre&gt;";
?&gt;
Copy linkTweet thisAlerts:
@TJ111Sep 24.2007 — You don't "need" SSH access to do this. The error you were reporting earlier with the "permission denied" means that you don't have access to write to the directory. This could be caused by using your entire root file location in where to place the file. Try using relative file paths instead of absolute file paths.
Copy linkTweet thisAlerts:
@steventowtauthorSep 24.2007 — so just using the path "/uploads/", I get this:
<i>
</i>Notice: Undefined index: filename in /home/stevento/public_html/selifx/upload.php on line 3

Warning: move_uploaded_file(): open_basedir restriction in effect. File(/uploads/preview.jpg) is not within the allowed path(s): (/home/stevento/:/usr/lib/php:/usr/local/lib/php:/tmp:/var/tmp:/home/vdeck/tmp/:/usr/local/bin/mogrify:/usr/local/bin/convert:/usr/sbin/sendmail) in /home/stevento/public_html/selifx/upload.php on line 11
upload failed! Here is some more info on your upload:Array ( [filename] =&gt; Array ( [name] =&gt; preview.jpg [type] =&gt; image/jpeg [tmp_name] =&gt; /var/tmp/php5JdXsT [error] =&gt; 0 [size] =&gt; 495656 ) )

using the php code above.
Copy linkTweet thisAlerts:
@TJ111Sep 24.2007 — Remove the preceding / from the file path. Use the path "uploads/", "../uploads/", "../../files/uploads", etc.
Copy linkTweet thisAlerts:
@steventowtauthorSep 24.2007 — now it works.

thanks a lot.

just a few more things?

how do I make it where it doesn't display this when upload is complete:
<i>
</i>Notice: Undefined index: filename in /home/stevento/public_html/selifx/upload.php on line 3


and how do I make the uploaded file have a random URL when uploaded???

test site:

http://steventowt.com/selifx
Copy linkTweet thisAlerts:
@hastxSep 25.2007 — To get rid of the notice, remove the line:
[code=php]
$filename = $_POST['filename'];
[/code]


To make sure the that two uploads don't have the same name, you could append the file name with a random number:
[code=php]
...
$file = stripslashes($file);
$file = rand().$file; //this will append a random number to the file name

$uploadfile = $uploaddir . $file;
...
[/code]
Copy linkTweet thisAlerts:
@steventowtauthorSep 25.2007 — thanks.

now that I have all of that settled.

the only thing I want now is when you upload a file, a new page is displayed, kinda like it is now, but with page style and stuff, like the page is before you upload. How would I do that and still be able to use the file name to display the URL.

http://steventowt.com/selifx
Copy linkTweet thisAlerts:
@hastxSep 25.2007 — You already have the script. just put your html styling below it, or put a link to a n html page

You don't need to actually echo the array info on the script page if you don't want...it's mostly for troubleshooting.
Copy linkTweet thisAlerts:
@steventowtauthorOct 25.2007 — thanks to those who helped me with this.

now I just have one problem, when I try to upload a large file, it doesn't upload.

I'm not for sure what the limit is.

anything I can add to the php file or anything to allow large file uploads???

site:

http://steventowt.com/upload
Copy linkTweet thisAlerts:
@TJ111Oct 26.2007 — It depends how much control you have over your site hosting. In the php.ini file, there is a settings called post_max_size and upload_max_filesize that sets how large a file can be uploaded to the server. To check, make a script with only the follwing:
[code=php]
echo "post_max_size=" .ini_get('post_max_size') ."<br>"; //returns the integer value
echo ini_get('upload_max_filesize'); //returns full string, not an integer value
[/code]


If you don't have the ability to change this via ini_set() or in the php.ini file itself, contact your web host about it.
×

Success!

Help @steventowt 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.12,
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,
)...