insert imagelocation
Ik ben bezig met het maken van een script waarbij ik een bestand kan uploaden en de locatie ervan kan invoegen in de database. het uploaden lukt, maar het invoegen in database niet.
Hier is de script
Quote:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
include("connect.php");
$_SESSION['username']="alex";
$username = $_SESSION['username'];
$MAX_SIZE = 200000000000;
//Allowable file Mime Types. Add more mime types if you want
$FILE_MIMES = array('application/pdf','video/mpg','video/avi','image/jpeg','image/jpg','image/gif'
,'image/png','application/msword');
//Allowable file ext. names. you may add more extension names.
$FILE_EXTS = array('.pdf','.txt','.zip','.jpg','.png','.gif','.avi','.mpg','.mpeg','.asf');
if(@$_POST['submit'])
{
//get file attributes
$name = $_FILES['myfile']['name'];
$file_type = $_FILES['myfile']['type'];
$tmp_name = $_FILES['myfile']['tmp_name'];
if($name)
{
//start upload process
$location = "video/$name";
move_uploaded_file($tmp_name,$location);
if ( $_FILES['myfile']['size'] > $MAX_SIZE)
$message = "The file size is over 2MB.";
//File Type/Extension Check
else if (!in_array($file_type, $FILE_MIMES)
&&!in_array($file_ext, $FILE_EXTS) )
$message = "Sorry, $name($file_type) is not allowed to be uploaded.";
else
$query = "INSERT INTO users(username, ìmagelocation) VALUES('$username','$location')";
die("your video has been uploaded!");
}
else
die("please select a file!");
}
echo "Welcome, ".$username."!<p>";
echo "Upload your video:
<form action='upload.php' method='POST' enctype='multipart/form-data'>
File: <input type='file' name='myfile'> <input type='submit' name='submit' value='Upload!'>
</form>
";
?>
include("connect.php");
$_SESSION['username']="alex";
$username = $_SESSION['username'];
$MAX_SIZE = 200000000000;
//Allowable file Mime Types. Add more mime types if you want
$FILE_MIMES = array('application/pdf','video/mpg','video/avi','image/jpeg','image/jpg','image/gif'
,'image/png','application/msword');
//Allowable file ext. names. you may add more extension names.
$FILE_EXTS = array('.pdf','.txt','.zip','.jpg','.png','.gif','.avi','.mpg','.mpeg','.asf');
if(@$_POST['submit'])
{
//get file attributes
$name = $_FILES['myfile']['name'];
$file_type = $_FILES['myfile']['type'];
$tmp_name = $_FILES['myfile']['tmp_name'];
if($name)
{
//start upload process
$location = "video/$name";
move_uploaded_file($tmp_name,$location);
if ( $_FILES['myfile']['size'] > $MAX_SIZE)
$message = "The file size is over 2MB.";
//File Type/Extension Check
else if (!in_array($file_type, $FILE_MIMES)
&&!in_array($file_ext, $FILE_EXTS) )
$message = "Sorry, $name($file_type) is not allowed to be uploaded.";
else
$query = "INSERT INTO users(username, ìmagelocation) VALUES('$username','$location')";
die("your video has been uploaded!");
}
else
die("please select a file!");
}
echo "Welcome, ".$username."!<p>";
echo "Upload your video:
<form action='upload.php' method='POST' enctype='multipart/form-data'>
File: <input type='file' name='myfile'> <input type='submit' name='submit' value='Upload!'>
</form>
";
?>
Wat lukt er exact niet? Overigens is je code vrij rommelig.
het $locatie is de plaats waar het document naartoe wordt upgeload. deze locatie wil ik ook in de database opslaan. Hoe moet ik dat doen. ik heb de script hierboven geschreven. is de insert query goed?
Misschien moet je de query ook uitvoeren?
Foutafhandeling - Query - SQL
Tevens wat inspringingen gebruiken in je code, zodat het leesbaarder wordt.
bv. dit zou je toch echt moeten omdraaien :
Code (php)
1
2
3
4
2
3
4
...
move_uploaded_file($tmp_name,$location);
if ( $_FILES['myfile']['size'] > $MAX_SIZE)
...
move_uploaded_file($tmp_name,$location);
if ( $_FILES['myfile']['size'] > $MAX_SIZE)
...
(eerst controleren of de file niet te groot is, dan pas opslaan op de server)
move_uploaded_file moet op de zelfde plaats komen als
$query = "INSERT INTO users(username, ìmagelocation) ....
En dan uiteraard de query uitvoeren, zoals SanThe zegt.
Kijk trouwens eens naar die eerste letter van ìmagelocation. Ik veronderstel dat dat accent niet de bedoeling is.
Weten jullie hoe ik dat voor moet aanpakken.
dat is mijn script.
Formulier.php
Quote:
<form enctype="multipart/form-data" action="upload.php" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br>
Phone: <input type="text" name = "phone"><br>
Photo: <input type="file" name="photo"><br>
<input type="submit" value="Add">
</form>
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br>
Phone: <input type="text" name = "phone"><br>
Photo: <input type="file" name="photo"><br>
<input type="submit" value="Add">
</form>
upload.php
Quote:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
//This is the directory where images will be saved
$MAX_SIZE = 200000000000;
//Allowable file Mime Types. Add more mime types if you want
$FILE_MIMES = array('application/pdf','video/mpg','video/avi','image/jpeg','image/jpg','image/gif'
,'image/png','application/msword');
//Allowable file ext. names. you may add more extension names.
$FILE_EXTS = array('.pdf','.txt','.zip','.jpg','.png','.gif','.avi','.mpg','.mpeg','.asf');
$target = "files/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']);
$file_type=($_FILES['photo']['type']);
$file_ext = strtolower(substr($pic,strrpos($pic,".")));
// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("phpacademy") or die(mysql_error()) ;
//Writes the information to the database
if ( $_FILES['photo']['size'] > $MAX_SIZE)
$message = "The file size is over 2MB.";
//File Type/Extension Check
else if (!in_array($file_type, $FILE_MIMES)
&&!in_array($file_ext, $FILE_EXTS) )
$message = "Sorry, $name($file_type) is not allowed to be uploaded.";
else
mysql_query("INSERT INTO `employees`(name, email, phone, photo)VALUES ('$name', '$email', '$phone', '$pic')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
//This is the directory where images will be saved
$MAX_SIZE = 200000000000;
//Allowable file Mime Types. Add more mime types if you want
$FILE_MIMES = array('application/pdf','video/mpg','video/avi','image/jpeg','image/jpg','image/gif'
,'image/png','application/msword');
//Allowable file ext. names. you may add more extension names.
$FILE_EXTS = array('.pdf','.txt','.zip','.jpg','.png','.gif','.avi','.mpg','.mpeg','.asf');
$target = "files/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']);
$file_type=($_FILES['photo']['type']);
$file_ext = strtolower(substr($pic,strrpos($pic,".")));
// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("phpacademy") or die(mysql_error()) ;
//Writes the information to the database
if ( $_FILES['photo']['size'] > $MAX_SIZE)
$message = "The file size is over 2MB.";
//File Type/Extension Check
else if (!in_array($file_type, $FILE_MIMES)
&&!in_array($file_ext, $FILE_EXTS) )
$message = "Sorry, $name($file_type) is not allowed to be uploaded.";
else
mysql_query("INSERT INTO `employees`(name, email, phone, photo)VALUES ('$name', '$email', '$phone', '$pic')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
Gewijzigd op 05/07/2012 16:22:18 door george mendel