Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 06-16-2009, 12:15 AM Photo Upload
Junior Talker

Posts: 1
Name: Nate
Trades: 0
Hi all!

I have a fairly simple problem to most of you I am sure. This is giving me fits though. This code uploads a photo (checks if the name exists) and placed the photo name in my MYSQL db. Works great. however, i need to redirect to another page upon upload AND I would like to rename the photo based on form input variables..fist name, lastname and a random number.


Any ideas?




Code:
<form name="upload" enctype="multipart/form-data" method="post" action="">
<span class="style1">JMI REPORTS, INC. - ONLINE INSPECTOR APPLICATION </span>
<p><span class="as">We are always seeking qualified individuals who are interested in conducting field   inspections with JMI Reports. </span></p>
<p class="as">Currently, we have our greatest need in <a href="needsinspector.php" target="_blank">these areas. (click here)</a></p>
<p class="as">Although we may not have an immediate need in   the area you live, you are welcome to submit an application for future consideration.</p>
<p class="as"><strong>THIS PROCESS WILL TAKE ABOUT 15 MINUTES AND INCLUDES A SHORT KNOWLEDGE ASSESSMENT.</strong></p>
<p>Request a username:
  <input type="text" name="username" />
<span class="gg">(please write this down) </span></p>
<p>Request a password:
  <label>
    <input type="text" name="password" id="password" />
  </label> 
<span class="gg">(please write this down) </span></p>
<p>
  Badge Photo Upload:
  <input type="file" name="file" />
</p>
<p class="gg"><strong>If you do not have a photo immediately available, put in your requested username and password and hit upload</strong>. </p>
<p class="gg"><strong>Then  click CONTINUE below to login and completed the registration process.</strong></p>
<p>
  <input type="submit" name="submit" value="Upload" />
<span class="as"><strong><a href="initlogin.php">CONTINUE</a></strong></span></p>
<p>
  <?php

if(isset($_POST['submit'])) {

$dir = "/home/content/n/w/c/nwclark/html/jmi2/secure/idphotos/"; //Upload directory
$error = ""; //Setting a false error
$address = "http://97.74.52.5/jmi2/".$_SERVER['HTTP_HOST']."/"; //Getting the web address
$username=$_POST['username'];
$password=$_POST['password'];
$file_name = $_FILES['file']['name']; //Getting the file name
$file_type = $_FILES['file']['type']; //Getting the file type
$file_size = "".$_FILES['file']['size']." bytes"; //Getting the file size
$file_tmp = $_FILES['file']['tmp_name']; //Setting the temporary name
$file_address = $address.$dir.$file_name; //URL of file
?>
  <?
mysql_query("INSERT INTO jmi10.user (username,photo,password) VALUES ('$username', '$file_name', '$password')") or die("Error: " . mysql_error());


if(file_exists($dir.$file_name)) {
$error = "<br />Error: A file with the same name already exists!";
}

else {
@copy ($file_tmp, $dir.$file_name) or ($error="<br />Error: File could not
be copied!");
}

if($error != "") {
echo $error;
}

else {
echo "<br />File successfully uploaded!\n";
echo "<br />Name: ".$file_name."\n";
echo "<br />Size: ".$file_size."\n";
echo "<br />Type: ".$file_type."\n";
}
}

?>
</p>
<p class="as">&nbsp;</p>
nwclark is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-22-2009, 12:47 PM Re: Photo Upload
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
For redirection, see http://www.php.net/header . As for the file name, you'll just change $file_name to match your rules.

Let us know if you need more guidance.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 06-25-2009, 06:26 PM Re: Photo Upload
Average Talker

Posts: 26
Name: Prithwiraj Bose
Location: Kalyani, West Bengal, India
Trades: 0
PHP header redirection will not work here. Because, the script has already started output, before the scope of header function arrives.

The updated code is given below.

Code:
 
<form name="upload" enctype="multipart/form-data" method="post" action="">
<span class="style1">JMI REPORTS, INC. - ONLINE INSPECTOR APPLICATION </span>
<p><span class="as">We are always seeking qualified individuals who are interested in conducting field   inspections with JMI Reports. </span></p>
<p class="as">Currently, we have our greatest need in <a href="needsinspector.php" target="_blank">these areas. (click here)</a></p>
<p class="as">Although we may not have an immediate need in   the area you live, you are welcome to submit an application for future consideration.</p>
<p class="as"><strong>THIS PROCESS WILL TAKE ABOUT 15 MINUTES AND INCLUDES A SHORT KNOWLEDGE ASSESSMENT.</strong></p>
<p>Request a username:
  <input type="text" name="username" />
<span class="gg">(please write this down) </span></p>
<p>Request a password:
  <label>
    <input type="text" name="password" id="password" />
  </label> 
<span class="gg">(please write this down) </span></p>
<p>
  Badge Photo Upload:
  <input type="file" name="file" />
</p>
<p class="gg"><strong>If you do not have a photo immediately available, put in your requested username and password and hit upload</strong>. </p>
<p class="gg"><strong>Then  click CONTINUE below to login and completed the registration process.</strong></p>
<p>
  <input type="submit" name="submit" value="Upload" />
<span class="as"><strong><a href="initlogin.php">CONTINUE</a></strong></span></p>
<p>
  <?php

if(isset($_POST['submit'])) {

$dir = "/home/content/n/w/c/nwclark/html/jmi2/secure/idphotos/"; //Upload directory
$error = ""; //Setting a false error
$address = "http://97.74.52.5/jmi2/".$_SERVER['HTTP_HOST']."/"; //Getting the web address
 
$randomNumber= time();
$username=$_POST['username'];
$password=$_POST['password'];
$file_name = $randomNumber.$username.$_FILES['file']['name']; //Getting the file name
$file_type = $_FILES['file']['type']; //Getting the file type
$file_size = "".$_FILES['file']['size']." bytes"; //Getting the file size
$file_tmp = $_FILES['file']['tmp_name']; //Setting the temporary name
$file_address = $address.$dir.$file_name; //URL of file
?>
  <?
mysql_query("INSERT INTO jmi10.user (username,photo,password) VALUES ('$username', '$file_name', '$password')") or die("Error: " . mysql_error());


if(file_exists($dir.$file_name)) {
$error = "<br />Error: A file with the same name already exists!";
}

else {
@copy ($file_tmp, $dir.$file_name) or ($error="<br />Error: File could not
be copied!");
}

if($error != "") {
echo $error;
}

else {
echo "<br />File successfully uploaded!\n";
echo "<br />Name: ".$file_name."\n";
echo "<br />Size: ".$file_size."\n";
echo "<br />Type: ".$file_type."\n";
echo "<br />You're being redirected...";
?>
<script type='text/javascript'>
document.location="redirect_url_here";
</script>
 
<?php


}
}

?>
</p>
<p class="as">&nbsp;</p>

The red colored codes are changed (from the original) and the redirecting javascript code is added. The Grey code can be removed, if you don't want to show any upload successful notification.
__________________

Please login or register to view this content. Registration is FREE
in Kolkata
|
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
| Become a
Please login or register to view this content. Registration is FREE
for Free.
techbongo is offline
Reply With Quote
View Public Profile
 
Old 06-26-2009, 09:47 AM Re: Photo Upload
ATLChris's Avatar
Experienced Talker

Posts: 37
Trades: 0
I would suggest a meta redirect. If I were you though, I would break the php upload to a different page use header as your redirect.

For the rename use PHP rename():
PHP Code:
  <?php
rename
("/tmp/tmp_file.txt""/home/user/login/docs/my_file.txt");
?>
__________________
--

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

@ATLChris

Last edited by ATLChris; 06-26-2009 at 09:49 AM..
ATLChris is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Photo Upload
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.52567 seconds with 12 queries